@@ -217,7 +217,7 @@ |
||
217 | 217 | // interpolate $message as defined in PSR-3 |
218 | 218 | $replace = []; |
219 | 219 | foreach ($context as $key => $val) { |
220 | - $replace['{' . $key . '}'] = $val; |
|
220 | + $replace['{'.$key.'}'] = $val; |
|
221 | 221 | } |
222 | 222 | $message = strtr($message, $replace); |
223 | 223 |
@@ -57,325 +57,325 @@ |
||
57 | 57 | */ |
58 | 58 | class Log implements ILogger, IDataLogger { |
59 | 59 | |
60 | - /** @var IWriter */ |
|
61 | - private $logger; |
|
62 | - |
|
63 | - /** @var SystemConfig */ |
|
64 | - private $config; |
|
65 | - |
|
66 | - /** @var boolean|null cache the result of the log condition check for the request */ |
|
67 | - private $logConditionSatisfied = null; |
|
68 | - |
|
69 | - /** @var Normalizer */ |
|
70 | - private $normalizer; |
|
71 | - |
|
72 | - /** @var IRegistry */ |
|
73 | - private $crashReporters; |
|
74 | - |
|
75 | - /** |
|
76 | - * @param IWriter $logger The logger that should be used |
|
77 | - * @param SystemConfig $config the system config object |
|
78 | - * @param Normalizer|null $normalizer |
|
79 | - * @param IRegistry|null $registry |
|
80 | - */ |
|
81 | - public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
82 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
83 | - if ($config === null) { |
|
84 | - $config = \OC::$server->getSystemConfig(); |
|
85 | - } |
|
86 | - |
|
87 | - $this->config = $config; |
|
88 | - $this->logger = $logger; |
|
89 | - if ($normalizer === null) { |
|
90 | - $this->normalizer = new Normalizer(); |
|
91 | - } else { |
|
92 | - $this->normalizer = $normalizer; |
|
93 | - } |
|
94 | - $this->crashReporters = $registry; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * System is unusable. |
|
99 | - * |
|
100 | - * @param string $message |
|
101 | - * @param array $context |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - public function emergency(string $message, array $context = []) { |
|
105 | - $this->log(ILogger::FATAL, $message, $context); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Action must be taken immediately. |
|
110 | - * |
|
111 | - * Example: Entire website down, database unavailable, etc. This should |
|
112 | - * trigger the SMS alerts and wake you up. |
|
113 | - * |
|
114 | - * @param string $message |
|
115 | - * @param array $context |
|
116 | - * @return void |
|
117 | - */ |
|
118 | - public function alert(string $message, array $context = []) { |
|
119 | - $this->log(ILogger::ERROR, $message, $context); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Critical conditions. |
|
124 | - * |
|
125 | - * Example: Application component unavailable, unexpected exception. |
|
126 | - * |
|
127 | - * @param string $message |
|
128 | - * @param array $context |
|
129 | - * @return void |
|
130 | - */ |
|
131 | - public function critical(string $message, array $context = []) { |
|
132 | - $this->log(ILogger::ERROR, $message, $context); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Runtime errors that do not require immediate action but should typically |
|
137 | - * be logged and monitored. |
|
138 | - * |
|
139 | - * @param string $message |
|
140 | - * @param array $context |
|
141 | - * @return void |
|
142 | - */ |
|
143 | - public function error(string $message, array $context = []) { |
|
144 | - $this->log(ILogger::ERROR, $message, $context); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Exceptional occurrences that are not errors. |
|
149 | - * |
|
150 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
151 | - * that are not necessarily wrong. |
|
152 | - * |
|
153 | - * @param string $message |
|
154 | - * @param array $context |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - public function warning(string $message, array $context = []) { |
|
158 | - $this->log(ILogger::WARN, $message, $context); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Normal but significant events. |
|
163 | - * |
|
164 | - * @param string $message |
|
165 | - * @param array $context |
|
166 | - * @return void |
|
167 | - */ |
|
168 | - public function notice(string $message, array $context = []) { |
|
169 | - $this->log(ILogger::INFO, $message, $context); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Interesting events. |
|
174 | - * |
|
175 | - * Example: User logs in, SQL logs. |
|
176 | - * |
|
177 | - * @param string $message |
|
178 | - * @param array $context |
|
179 | - * @return void |
|
180 | - */ |
|
181 | - public function info(string $message, array $context = []) { |
|
182 | - $this->log(ILogger::INFO, $message, $context); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Detailed debug information. |
|
187 | - * |
|
188 | - * @param string $message |
|
189 | - * @param array $context |
|
190 | - * @return void |
|
191 | - */ |
|
192 | - public function debug(string $message, array $context = []) { |
|
193 | - $this->log(ILogger::DEBUG, $message, $context); |
|
194 | - } |
|
195 | - |
|
196 | - |
|
197 | - /** |
|
198 | - * Logs with an arbitrary level. |
|
199 | - * |
|
200 | - * @param int $level |
|
201 | - * @param string $message |
|
202 | - * @param array $context |
|
203 | - * @return void |
|
204 | - */ |
|
205 | - public function log(int $level, string $message, array $context = []) { |
|
206 | - $minLevel = $this->getLogLevel($context); |
|
207 | - |
|
208 | - array_walk($context, [$this->normalizer, 'format']); |
|
209 | - |
|
210 | - $app = $context['app'] ?? 'no app in context'; |
|
211 | - |
|
212 | - // interpolate $message as defined in PSR-3 |
|
213 | - $replace = []; |
|
214 | - foreach ($context as $key => $val) { |
|
215 | - $replace['{' . $key . '}'] = $val; |
|
216 | - } |
|
217 | - $message = strtr($message, $replace); |
|
218 | - |
|
219 | - try { |
|
220 | - if ($level >= $minLevel) { |
|
221 | - $this->writeLog($app, $message, $level); |
|
222 | - |
|
223 | - if ($this->crashReporters !== null) { |
|
224 | - $messageContext = array_merge( |
|
225 | - $context, |
|
226 | - [ |
|
227 | - 'level' => $level |
|
228 | - ] |
|
229 | - ); |
|
230 | - $this->crashReporters->delegateMessage($message, $messageContext); |
|
231 | - } |
|
232 | - } else { |
|
233 | - if ($this->crashReporters !== null) { |
|
234 | - $this->crashReporters->delegateBreadcrumb($message, 'log', $context); |
|
235 | - } |
|
236 | - } |
|
237 | - } catch (\Throwable $e) { |
|
238 | - // make sure we dont hard crash if logging fails |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - private function getLogLevel($context) { |
|
243 | - $logCondition = $this->config->getValue('log.condition', []); |
|
244 | - |
|
245 | - /** |
|
246 | - * check for a special log condition - this enables an increased log on |
|
247 | - * a per request/user base |
|
248 | - */ |
|
249 | - if ($this->logConditionSatisfied === null) { |
|
250 | - // default to false to just process this once per request |
|
251 | - $this->logConditionSatisfied = false; |
|
252 | - if (!empty($logCondition)) { |
|
253 | - |
|
254 | - // check for secret token in the request |
|
255 | - if (isset($logCondition['shared_secret'])) { |
|
256 | - $request = \OC::$server->getRequest(); |
|
257 | - |
|
258 | - if ($request->getMethod() === 'PUT' && |
|
259 | - strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false && |
|
260 | - strpos($request->getHeader('Content-Type'), 'application/json') === false) { |
|
261 | - $logSecretRequest = ''; |
|
262 | - } else { |
|
263 | - $logSecretRequest = $request->getParam('log_secret', ''); |
|
264 | - } |
|
265 | - |
|
266 | - // if token is found in the request change set the log condition to satisfied |
|
267 | - if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) { |
|
268 | - $this->logConditionSatisfied = true; |
|
269 | - } |
|
270 | - } |
|
271 | - |
|
272 | - // check for user |
|
273 | - if (isset($logCondition['users'])) { |
|
274 | - $user = \OC::$server->getUserSession()->getUser(); |
|
275 | - |
|
276 | - // if the user matches set the log condition to satisfied |
|
277 | - if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
278 | - $this->logConditionSatisfied = true; |
|
279 | - } |
|
280 | - } |
|
281 | - } |
|
282 | - } |
|
283 | - |
|
284 | - // if log condition is satisfied change the required log level to DEBUG |
|
285 | - if ($this->logConditionSatisfied) { |
|
286 | - return ILogger::DEBUG; |
|
287 | - } |
|
288 | - |
|
289 | - if (isset($context['app'])) { |
|
290 | - $app = $context['app']; |
|
291 | - |
|
292 | - /** |
|
293 | - * check log condition based on the context of each log message |
|
294 | - * once this is met -> change the required log level to debug |
|
295 | - */ |
|
296 | - if (!empty($logCondition) |
|
297 | - && isset($logCondition['apps']) |
|
298 | - && in_array($app, $logCondition['apps'], true)) { |
|
299 | - return ILogger::DEBUG; |
|
300 | - } |
|
301 | - } |
|
302 | - |
|
303 | - return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Logs an exception very detailed |
|
308 | - * |
|
309 | - * @param \Exception|\Throwable $exception |
|
310 | - * @param array $context |
|
311 | - * @return void |
|
312 | - * @since 8.2.0 |
|
313 | - */ |
|
314 | - public function logException(\Throwable $exception, array $context = []) { |
|
315 | - $app = $context['app'] ?? 'no app in context'; |
|
316 | - $level = $context['level'] ?? ILogger::ERROR; |
|
317 | - |
|
318 | - $serializer = new ExceptionSerializer(); |
|
319 | - $data = $serializer->serializeException($exception); |
|
320 | - $data['CustomMessage'] = $context['message'] ?? '--'; |
|
321 | - |
|
322 | - $minLevel = $this->getLogLevel($context); |
|
323 | - |
|
324 | - array_walk($context, [$this->normalizer, 'format']); |
|
325 | - |
|
326 | - try { |
|
327 | - if ($level >= $minLevel) { |
|
328 | - if (!$this->logger instanceof IFileBased) { |
|
329 | - $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
330 | - } |
|
331 | - $this->writeLog($app, $data, $level); |
|
332 | - } |
|
333 | - |
|
334 | - $context['level'] = $level; |
|
335 | - if (!is_null($this->crashReporters)) { |
|
336 | - $this->crashReporters->delegateReport($exception, $context); |
|
337 | - } |
|
338 | - } catch (\Throwable $e) { |
|
339 | - // make sure we dont hard crash if logging fails |
|
340 | - } |
|
341 | - } |
|
342 | - |
|
343 | - public function logData(string $message, array $data, array $context = []): void { |
|
344 | - $app = $context['app'] ?? 'no app in context'; |
|
345 | - $level = $context['level'] ?? ILogger::ERROR; |
|
346 | - |
|
347 | - $minLevel = $this->getLogLevel($context); |
|
348 | - |
|
349 | - array_walk($context, [$this->normalizer, 'format']); |
|
350 | - |
|
351 | - try { |
|
352 | - if ($level >= $minLevel) { |
|
353 | - $data['message'] = $message; |
|
354 | - if (!$this->logger instanceof IFileBased) { |
|
355 | - $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
356 | - } |
|
357 | - $this->writeLog($app, $data, $level); |
|
358 | - } |
|
359 | - |
|
360 | - $context['level'] = $level; |
|
361 | - } catch (\Throwable $e) { |
|
362 | - // make sure we dont hard crash if logging fails |
|
363 | - } |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * @param string $app |
|
368 | - * @param string|array $entry |
|
369 | - * @param int $level |
|
370 | - */ |
|
371 | - protected function writeLog(string $app, $entry, int $level) { |
|
372 | - $this->logger->write($app, $entry, $level); |
|
373 | - } |
|
374 | - |
|
375 | - public function getLogPath():string { |
|
376 | - if ($this->logger instanceof IFileBased) { |
|
377 | - return $this->logger->getLogFilePath(); |
|
378 | - } |
|
379 | - throw new \RuntimeException('Log implementation has no path'); |
|
380 | - } |
|
60 | + /** @var IWriter */ |
|
61 | + private $logger; |
|
62 | + |
|
63 | + /** @var SystemConfig */ |
|
64 | + private $config; |
|
65 | + |
|
66 | + /** @var boolean|null cache the result of the log condition check for the request */ |
|
67 | + private $logConditionSatisfied = null; |
|
68 | + |
|
69 | + /** @var Normalizer */ |
|
70 | + private $normalizer; |
|
71 | + |
|
72 | + /** @var IRegistry */ |
|
73 | + private $crashReporters; |
|
74 | + |
|
75 | + /** |
|
76 | + * @param IWriter $logger The logger that should be used |
|
77 | + * @param SystemConfig $config the system config object |
|
78 | + * @param Normalizer|null $normalizer |
|
79 | + * @param IRegistry|null $registry |
|
80 | + */ |
|
81 | + public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
82 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
83 | + if ($config === null) { |
|
84 | + $config = \OC::$server->getSystemConfig(); |
|
85 | + } |
|
86 | + |
|
87 | + $this->config = $config; |
|
88 | + $this->logger = $logger; |
|
89 | + if ($normalizer === null) { |
|
90 | + $this->normalizer = new Normalizer(); |
|
91 | + } else { |
|
92 | + $this->normalizer = $normalizer; |
|
93 | + } |
|
94 | + $this->crashReporters = $registry; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * System is unusable. |
|
99 | + * |
|
100 | + * @param string $message |
|
101 | + * @param array $context |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + public function emergency(string $message, array $context = []) { |
|
105 | + $this->log(ILogger::FATAL, $message, $context); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Action must be taken immediately. |
|
110 | + * |
|
111 | + * Example: Entire website down, database unavailable, etc. This should |
|
112 | + * trigger the SMS alerts and wake you up. |
|
113 | + * |
|
114 | + * @param string $message |
|
115 | + * @param array $context |
|
116 | + * @return void |
|
117 | + */ |
|
118 | + public function alert(string $message, array $context = []) { |
|
119 | + $this->log(ILogger::ERROR, $message, $context); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Critical conditions. |
|
124 | + * |
|
125 | + * Example: Application component unavailable, unexpected exception. |
|
126 | + * |
|
127 | + * @param string $message |
|
128 | + * @param array $context |
|
129 | + * @return void |
|
130 | + */ |
|
131 | + public function critical(string $message, array $context = []) { |
|
132 | + $this->log(ILogger::ERROR, $message, $context); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Runtime errors that do not require immediate action but should typically |
|
137 | + * be logged and monitored. |
|
138 | + * |
|
139 | + * @param string $message |
|
140 | + * @param array $context |
|
141 | + * @return void |
|
142 | + */ |
|
143 | + public function error(string $message, array $context = []) { |
|
144 | + $this->log(ILogger::ERROR, $message, $context); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Exceptional occurrences that are not errors. |
|
149 | + * |
|
150 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
151 | + * that are not necessarily wrong. |
|
152 | + * |
|
153 | + * @param string $message |
|
154 | + * @param array $context |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + public function warning(string $message, array $context = []) { |
|
158 | + $this->log(ILogger::WARN, $message, $context); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Normal but significant events. |
|
163 | + * |
|
164 | + * @param string $message |
|
165 | + * @param array $context |
|
166 | + * @return void |
|
167 | + */ |
|
168 | + public function notice(string $message, array $context = []) { |
|
169 | + $this->log(ILogger::INFO, $message, $context); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Interesting events. |
|
174 | + * |
|
175 | + * Example: User logs in, SQL logs. |
|
176 | + * |
|
177 | + * @param string $message |
|
178 | + * @param array $context |
|
179 | + * @return void |
|
180 | + */ |
|
181 | + public function info(string $message, array $context = []) { |
|
182 | + $this->log(ILogger::INFO, $message, $context); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Detailed debug information. |
|
187 | + * |
|
188 | + * @param string $message |
|
189 | + * @param array $context |
|
190 | + * @return void |
|
191 | + */ |
|
192 | + public function debug(string $message, array $context = []) { |
|
193 | + $this->log(ILogger::DEBUG, $message, $context); |
|
194 | + } |
|
195 | + |
|
196 | + |
|
197 | + /** |
|
198 | + * Logs with an arbitrary level. |
|
199 | + * |
|
200 | + * @param int $level |
|
201 | + * @param string $message |
|
202 | + * @param array $context |
|
203 | + * @return void |
|
204 | + */ |
|
205 | + public function log(int $level, string $message, array $context = []) { |
|
206 | + $minLevel = $this->getLogLevel($context); |
|
207 | + |
|
208 | + array_walk($context, [$this->normalizer, 'format']); |
|
209 | + |
|
210 | + $app = $context['app'] ?? 'no app in context'; |
|
211 | + |
|
212 | + // interpolate $message as defined in PSR-3 |
|
213 | + $replace = []; |
|
214 | + foreach ($context as $key => $val) { |
|
215 | + $replace['{' . $key . '}'] = $val; |
|
216 | + } |
|
217 | + $message = strtr($message, $replace); |
|
218 | + |
|
219 | + try { |
|
220 | + if ($level >= $minLevel) { |
|
221 | + $this->writeLog($app, $message, $level); |
|
222 | + |
|
223 | + if ($this->crashReporters !== null) { |
|
224 | + $messageContext = array_merge( |
|
225 | + $context, |
|
226 | + [ |
|
227 | + 'level' => $level |
|
228 | + ] |
|
229 | + ); |
|
230 | + $this->crashReporters->delegateMessage($message, $messageContext); |
|
231 | + } |
|
232 | + } else { |
|
233 | + if ($this->crashReporters !== null) { |
|
234 | + $this->crashReporters->delegateBreadcrumb($message, 'log', $context); |
|
235 | + } |
|
236 | + } |
|
237 | + } catch (\Throwable $e) { |
|
238 | + // make sure we dont hard crash if logging fails |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + private function getLogLevel($context) { |
|
243 | + $logCondition = $this->config->getValue('log.condition', []); |
|
244 | + |
|
245 | + /** |
|
246 | + * check for a special log condition - this enables an increased log on |
|
247 | + * a per request/user base |
|
248 | + */ |
|
249 | + if ($this->logConditionSatisfied === null) { |
|
250 | + // default to false to just process this once per request |
|
251 | + $this->logConditionSatisfied = false; |
|
252 | + if (!empty($logCondition)) { |
|
253 | + |
|
254 | + // check for secret token in the request |
|
255 | + if (isset($logCondition['shared_secret'])) { |
|
256 | + $request = \OC::$server->getRequest(); |
|
257 | + |
|
258 | + if ($request->getMethod() === 'PUT' && |
|
259 | + strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false && |
|
260 | + strpos($request->getHeader('Content-Type'), 'application/json') === false) { |
|
261 | + $logSecretRequest = ''; |
|
262 | + } else { |
|
263 | + $logSecretRequest = $request->getParam('log_secret', ''); |
|
264 | + } |
|
265 | + |
|
266 | + // if token is found in the request change set the log condition to satisfied |
|
267 | + if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) { |
|
268 | + $this->logConditionSatisfied = true; |
|
269 | + } |
|
270 | + } |
|
271 | + |
|
272 | + // check for user |
|
273 | + if (isset($logCondition['users'])) { |
|
274 | + $user = \OC::$server->getUserSession()->getUser(); |
|
275 | + |
|
276 | + // if the user matches set the log condition to satisfied |
|
277 | + if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
278 | + $this->logConditionSatisfied = true; |
|
279 | + } |
|
280 | + } |
|
281 | + } |
|
282 | + } |
|
283 | + |
|
284 | + // if log condition is satisfied change the required log level to DEBUG |
|
285 | + if ($this->logConditionSatisfied) { |
|
286 | + return ILogger::DEBUG; |
|
287 | + } |
|
288 | + |
|
289 | + if (isset($context['app'])) { |
|
290 | + $app = $context['app']; |
|
291 | + |
|
292 | + /** |
|
293 | + * check log condition based on the context of each log message |
|
294 | + * once this is met -> change the required log level to debug |
|
295 | + */ |
|
296 | + if (!empty($logCondition) |
|
297 | + && isset($logCondition['apps']) |
|
298 | + && in_array($app, $logCondition['apps'], true)) { |
|
299 | + return ILogger::DEBUG; |
|
300 | + } |
|
301 | + } |
|
302 | + |
|
303 | + return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Logs an exception very detailed |
|
308 | + * |
|
309 | + * @param \Exception|\Throwable $exception |
|
310 | + * @param array $context |
|
311 | + * @return void |
|
312 | + * @since 8.2.0 |
|
313 | + */ |
|
314 | + public function logException(\Throwable $exception, array $context = []) { |
|
315 | + $app = $context['app'] ?? 'no app in context'; |
|
316 | + $level = $context['level'] ?? ILogger::ERROR; |
|
317 | + |
|
318 | + $serializer = new ExceptionSerializer(); |
|
319 | + $data = $serializer->serializeException($exception); |
|
320 | + $data['CustomMessage'] = $context['message'] ?? '--'; |
|
321 | + |
|
322 | + $minLevel = $this->getLogLevel($context); |
|
323 | + |
|
324 | + array_walk($context, [$this->normalizer, 'format']); |
|
325 | + |
|
326 | + try { |
|
327 | + if ($level >= $minLevel) { |
|
328 | + if (!$this->logger instanceof IFileBased) { |
|
329 | + $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
330 | + } |
|
331 | + $this->writeLog($app, $data, $level); |
|
332 | + } |
|
333 | + |
|
334 | + $context['level'] = $level; |
|
335 | + if (!is_null($this->crashReporters)) { |
|
336 | + $this->crashReporters->delegateReport($exception, $context); |
|
337 | + } |
|
338 | + } catch (\Throwable $e) { |
|
339 | + // make sure we dont hard crash if logging fails |
|
340 | + } |
|
341 | + } |
|
342 | + |
|
343 | + public function logData(string $message, array $data, array $context = []): void { |
|
344 | + $app = $context['app'] ?? 'no app in context'; |
|
345 | + $level = $context['level'] ?? ILogger::ERROR; |
|
346 | + |
|
347 | + $minLevel = $this->getLogLevel($context); |
|
348 | + |
|
349 | + array_walk($context, [$this->normalizer, 'format']); |
|
350 | + |
|
351 | + try { |
|
352 | + if ($level >= $minLevel) { |
|
353 | + $data['message'] = $message; |
|
354 | + if (!$this->logger instanceof IFileBased) { |
|
355 | + $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
356 | + } |
|
357 | + $this->writeLog($app, $data, $level); |
|
358 | + } |
|
359 | + |
|
360 | + $context['level'] = $level; |
|
361 | + } catch (\Throwable $e) { |
|
362 | + // make sure we dont hard crash if logging fails |
|
363 | + } |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * @param string $app |
|
368 | + * @param string|array $entry |
|
369 | + * @param int $level |
|
370 | + */ |
|
371 | + protected function writeLog(string $app, $entry, int $level) { |
|
372 | + $this->logger->write($app, $entry, $level); |
|
373 | + } |
|
374 | + |
|
375 | + public function getLogPath():string { |
|
376 | + if ($this->logger instanceof IFileBased) { |
|
377 | + return $this->logger->getLogFilePath(); |
|
378 | + } |
|
379 | + throw new \RuntimeException('Log implementation has no path'); |
|
380 | + } |
|
381 | 381 | } |
@@ -62,7 +62,7 @@ |
||
62 | 62 | */ |
63 | 63 | public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
64 | 64 | if (isset($this->collections[$name])) { |
65 | - throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
65 | + throw new \OutOfBoundsException('Duplicate entity name "'.$name.'"'); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | $this->collections[$name] = $entityExistsFunction; |
@@ -36,46 +36,46 @@ |
||
36 | 36 | * @since 9.1.0 |
37 | 37 | */ |
38 | 38 | class SystemTagsEntityEvent extends Event { |
39 | - public const EVENT_ENTITY = 'OCP\SystemTag\ISystemTagManager::registerEntity'; |
|
39 | + public const EVENT_ENTITY = 'OCP\SystemTag\ISystemTagManager::registerEntity'; |
|
40 | 40 | |
41 | - /** @var string */ |
|
42 | - protected $event; |
|
43 | - /** @var \Closure[] */ |
|
44 | - protected $collections; |
|
41 | + /** @var string */ |
|
42 | + protected $event; |
|
43 | + /** @var \Closure[] */ |
|
44 | + protected $collections; |
|
45 | 45 | |
46 | - /** |
|
47 | - * SystemTagsEntityEvent constructor. |
|
48 | - * |
|
49 | - * @param string $event |
|
50 | - * @since 9.1.0 |
|
51 | - */ |
|
52 | - public function __construct(string $event) { |
|
53 | - $this->event = $event; |
|
54 | - $this->collections = []; |
|
55 | - } |
|
46 | + /** |
|
47 | + * SystemTagsEntityEvent constructor. |
|
48 | + * |
|
49 | + * @param string $event |
|
50 | + * @since 9.1.0 |
|
51 | + */ |
|
52 | + public function __construct(string $event) { |
|
53 | + $this->event = $event; |
|
54 | + $this->collections = []; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * @param string $name |
|
59 | - * @param \Closure $entityExistsFunction The closure should take one |
|
60 | - * argument, which is the id of the entity, that tags |
|
61 | - * should be handled for. The return should then be bool, |
|
62 | - * depending on whether tags are allowed (true) or not. |
|
63 | - * @throws \OutOfBoundsException when the entity name is already taken |
|
64 | - * @since 9.1.0 |
|
65 | - */ |
|
66 | - public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
|
67 | - if (isset($this->collections[$name])) { |
|
68 | - throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
69 | - } |
|
57 | + /** |
|
58 | + * @param string $name |
|
59 | + * @param \Closure $entityExistsFunction The closure should take one |
|
60 | + * argument, which is the id of the entity, that tags |
|
61 | + * should be handled for. The return should then be bool, |
|
62 | + * depending on whether tags are allowed (true) or not. |
|
63 | + * @throws \OutOfBoundsException when the entity name is already taken |
|
64 | + * @since 9.1.0 |
|
65 | + */ |
|
66 | + public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
|
67 | + if (isset($this->collections[$name])) { |
|
68 | + throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
69 | + } |
|
70 | 70 | |
71 | - $this->collections[$name] = $entityExistsFunction; |
|
72 | - } |
|
71 | + $this->collections[$name] = $entityExistsFunction; |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * @return \Closure[] |
|
76 | - * @since 9.1.0 |
|
77 | - */ |
|
78 | - public function getEntityCollections(): array { |
|
79 | - return $this->collections; |
|
80 | - } |
|
74 | + /** |
|
75 | + * @return \Closure[] |
|
76 | + * @since 9.1.0 |
|
77 | + */ |
|
78 | + public function getEntityCollections(): array { |
|
79 | + return $this->collections; |
|
80 | + } |
|
81 | 81 | } |
@@ -29,14 +29,14 @@ |
||
29 | 29 | |
30 | 30 | class Errorlog implements IWriter { |
31 | 31 | |
32 | - /** |
|
33 | - * write a message in the log |
|
34 | - * @param string $app |
|
35 | - * @param string $message |
|
36 | - * @param int $level |
|
37 | - */ |
|
38 | - public function write(string $app, $message, int $level) { |
|
39 | - error_log('[owncloud]['.$app.']['.$level.'] '.$message); |
|
40 | - } |
|
32 | + /** |
|
33 | + * write a message in the log |
|
34 | + * @param string $app |
|
35 | + * @param string $message |
|
36 | + * @param int $level |
|
37 | + */ |
|
38 | + public function write(string $app, $message, int $level) { |
|
39 | + error_log('[owncloud]['.$app.']['.$level.'] '.$message); |
|
40 | + } |
|
41 | 41 | } |
42 | 42 |
@@ -30,8 +30,8 @@ |
||
30 | 30 | * @since 14.0.0 |
31 | 31 | */ |
32 | 32 | interface IWriter { |
33 | - /** |
|
34 | - * @since 14.0.0 |
|
35 | - */ |
|
36 | - public function write(string $app, $message, int $level); |
|
33 | + /** |
|
34 | + * @since 14.0.0 |
|
35 | + */ |
|
36 | + public function write(string $app, $message, int $level); |
|
37 | 37 | } |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | protected function run($argument) { |
155 | 155 | |
156 | 156 | $target = $argument['url']; |
157 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
157 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->timeFactory->getTime(); |
|
158 | 158 | $currentTime = $this->timeFactory->getTime(); |
159 | 159 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
160 | 160 | $source = rtrim($source, '/'); |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
173 | 173 | |
174 | 174 | // make sure that we have a well formated url |
175 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
175 | + $url = rtrim($target, '/').'/'.trim($endPoint, '/'); |
|
176 | 176 | |
177 | 177 | try { |
178 | 178 | $result = $this->httpClient->post( |
@@ -193,16 +193,16 @@ discard block |
||
193 | 193 | } catch (ClientException $e) { |
194 | 194 | $status = $e->getCode(); |
195 | 195 | if ($status === Http::STATUS_FORBIDDEN) { |
196 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
196 | + $this->logger->info($target.' refused to ask for a shared secret.', ['app' => 'federation']); |
|
197 | 197 | } else { |
198 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
198 | + $this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']); |
|
199 | 199 | } |
200 | 200 | } catch (RequestException $e) { |
201 | 201 | $status = -1; // There is no status code if we could not connect |
202 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
202 | + $this->logger->info('Could not connect to '.$target, ['app' => 'federation']); |
|
203 | 203 | } catch (RingException $e) { |
204 | 204 | $status = -1; // There is no status code if we could not connect |
205 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
205 | + $this->logger->info('Could not connect to '.$target, ['app' => 'federation']); |
|
206 | 206 | } catch (\Exception $e) { |
207 | 207 | $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
208 | 208 | $this->logger->logException($e, ['app' => 'federation']); |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | */ |
226 | 226 | protected function reAddJob(array $argument) { |
227 | 227 | $url = $argument['url']; |
228 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
228 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->timeFactory->getTime(); |
|
229 | 229 | $token = $argument['token']; |
230 | 230 | |
231 | 231 | $this->jobList->add( |
@@ -52,176 +52,176 @@ |
||
52 | 52 | */ |
53 | 53 | class RequestSharedSecret extends Job { |
54 | 54 | |
55 | - /** @var IClient */ |
|
56 | - private $httpClient; |
|
57 | - |
|
58 | - /** @var IJobList */ |
|
59 | - private $jobList; |
|
60 | - |
|
61 | - /** @var IURLGenerator */ |
|
62 | - private $urlGenerator; |
|
63 | - |
|
64 | - /** @var TrustedServers */ |
|
65 | - private $trustedServers; |
|
66 | - |
|
67 | - /** @var IDiscoveryService */ |
|
68 | - private $ocsDiscoveryService; |
|
69 | - |
|
70 | - /** @var ILogger */ |
|
71 | - private $logger; |
|
72 | - |
|
73 | - /** @var ITimeFactory */ |
|
74 | - private $timeFactory; |
|
75 | - |
|
76 | - /** @var bool */ |
|
77 | - protected $retainJob = false; |
|
78 | - |
|
79 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; |
|
80 | - |
|
81 | - /** @var int 30 day = 2592000sec */ |
|
82 | - private $maxLifespan = 2592000; |
|
83 | - |
|
84 | - /** |
|
85 | - * RequestSharedSecret constructor. |
|
86 | - * |
|
87 | - * @param IClientService $httpClientService |
|
88 | - * @param IURLGenerator $urlGenerator |
|
89 | - * @param IJobList $jobList |
|
90 | - * @param TrustedServers $trustedServers |
|
91 | - * @param IDiscoveryService $ocsDiscoveryService |
|
92 | - * @param ILogger $logger |
|
93 | - * @param ITimeFactory $timeFactory |
|
94 | - */ |
|
95 | - public function __construct( |
|
96 | - IClientService $httpClientService, |
|
97 | - IURLGenerator $urlGenerator, |
|
98 | - IJobList $jobList, |
|
99 | - TrustedServers $trustedServers, |
|
100 | - IDiscoveryService $ocsDiscoveryService, |
|
101 | - ILogger $logger, |
|
102 | - ITimeFactory $timeFactory |
|
103 | - ) { |
|
104 | - $this->httpClient = $httpClientService->newClient(); |
|
105 | - $this->jobList = $jobList; |
|
106 | - $this->urlGenerator = $urlGenerator; |
|
107 | - $this->logger = $logger; |
|
108 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
109 | - $this->trustedServers = $trustedServers; |
|
110 | - $this->timeFactory = $timeFactory; |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * run the job, then remove it from the joblist |
|
116 | - * |
|
117 | - * @param JobList $jobList |
|
118 | - * @param ILogger|null $logger |
|
119 | - */ |
|
120 | - public function execute($jobList, ILogger $logger = null) { |
|
121 | - $target = $this->argument['url']; |
|
122 | - // only execute if target is still in the list of trusted domains |
|
123 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
124 | - $this->parentExecute($jobList, $logger); |
|
125 | - } |
|
126 | - |
|
127 | - $jobList->remove($this, $this->argument); |
|
128 | - |
|
129 | - if ($this->retainJob) { |
|
130 | - $this->reAddJob($this->argument); |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * call execute() method of parent |
|
136 | - * |
|
137 | - * @param JobList $jobList |
|
138 | - * @param ILogger $logger |
|
139 | - */ |
|
140 | - protected function parentExecute($jobList, $logger) { |
|
141 | - parent::execute($jobList, $logger); |
|
142 | - } |
|
143 | - |
|
144 | - protected function run($argument) { |
|
145 | - $target = $argument['url']; |
|
146 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
147 | - $currentTime = $this->timeFactory->getTime(); |
|
148 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
149 | - $source = rtrim($source, '/'); |
|
150 | - $token = $argument['token']; |
|
151 | - |
|
152 | - // kill job after 30 days of trying |
|
153 | - $deadline = $currentTime - $this->maxLifespan; |
|
154 | - if ($created < $deadline) { |
|
155 | - $this->retainJob = false; |
|
156 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
157 | - return; |
|
158 | - } |
|
159 | - |
|
160 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
161 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
162 | - |
|
163 | - // make sure that we have a well formated url |
|
164 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
165 | - |
|
166 | - try { |
|
167 | - $result = $this->httpClient->post( |
|
168 | - $url, |
|
169 | - [ |
|
170 | - 'body' => [ |
|
171 | - 'url' => $source, |
|
172 | - 'token' => $token, |
|
173 | - 'format' => 'json', |
|
174 | - ], |
|
175 | - 'timeout' => 3, |
|
176 | - 'connect_timeout' => 3, |
|
177 | - ] |
|
178 | - ); |
|
179 | - |
|
180 | - $status = $result->getStatusCode(); |
|
181 | - } catch (ClientException $e) { |
|
182 | - $status = $e->getCode(); |
|
183 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
184 | - $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
185 | - } else { |
|
186 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
187 | - } |
|
188 | - } catch (RequestException $e) { |
|
189 | - $status = -1; // There is no status code if we could not connect |
|
190 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
191 | - } catch (RingException $e) { |
|
192 | - $status = -1; // There is no status code if we could not connect |
|
193 | - $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
194 | - } catch (\Exception $e) { |
|
195 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
196 | - $this->logger->logException($e, ['app' => 'federation']); |
|
197 | - } |
|
198 | - |
|
199 | - // if we received a unexpected response we try again later |
|
200 | - if ( |
|
201 | - $status !== Http::STATUS_OK |
|
202 | - && $status !== Http::STATUS_FORBIDDEN |
|
203 | - ) { |
|
204 | - $this->retainJob = true; |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * re-add background job |
|
210 | - * |
|
211 | - * @param array $argument |
|
212 | - */ |
|
213 | - protected function reAddJob(array $argument) { |
|
214 | - $url = $argument['url']; |
|
215 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
216 | - $token = $argument['token']; |
|
217 | - |
|
218 | - $this->jobList->add( |
|
219 | - RequestSharedSecret::class, |
|
220 | - [ |
|
221 | - 'url' => $url, |
|
222 | - 'token' => $token, |
|
223 | - 'created' => $created |
|
224 | - ] |
|
225 | - ); |
|
226 | - } |
|
55 | + /** @var IClient */ |
|
56 | + private $httpClient; |
|
57 | + |
|
58 | + /** @var IJobList */ |
|
59 | + private $jobList; |
|
60 | + |
|
61 | + /** @var IURLGenerator */ |
|
62 | + private $urlGenerator; |
|
63 | + |
|
64 | + /** @var TrustedServers */ |
|
65 | + private $trustedServers; |
|
66 | + |
|
67 | + /** @var IDiscoveryService */ |
|
68 | + private $ocsDiscoveryService; |
|
69 | + |
|
70 | + /** @var ILogger */ |
|
71 | + private $logger; |
|
72 | + |
|
73 | + /** @var ITimeFactory */ |
|
74 | + private $timeFactory; |
|
75 | + |
|
76 | + /** @var bool */ |
|
77 | + protected $retainJob = false; |
|
78 | + |
|
79 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/request-shared-secret'; |
|
80 | + |
|
81 | + /** @var int 30 day = 2592000sec */ |
|
82 | + private $maxLifespan = 2592000; |
|
83 | + |
|
84 | + /** |
|
85 | + * RequestSharedSecret constructor. |
|
86 | + * |
|
87 | + * @param IClientService $httpClientService |
|
88 | + * @param IURLGenerator $urlGenerator |
|
89 | + * @param IJobList $jobList |
|
90 | + * @param TrustedServers $trustedServers |
|
91 | + * @param IDiscoveryService $ocsDiscoveryService |
|
92 | + * @param ILogger $logger |
|
93 | + * @param ITimeFactory $timeFactory |
|
94 | + */ |
|
95 | + public function __construct( |
|
96 | + IClientService $httpClientService, |
|
97 | + IURLGenerator $urlGenerator, |
|
98 | + IJobList $jobList, |
|
99 | + TrustedServers $trustedServers, |
|
100 | + IDiscoveryService $ocsDiscoveryService, |
|
101 | + ILogger $logger, |
|
102 | + ITimeFactory $timeFactory |
|
103 | + ) { |
|
104 | + $this->httpClient = $httpClientService->newClient(); |
|
105 | + $this->jobList = $jobList; |
|
106 | + $this->urlGenerator = $urlGenerator; |
|
107 | + $this->logger = $logger; |
|
108 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
109 | + $this->trustedServers = $trustedServers; |
|
110 | + $this->timeFactory = $timeFactory; |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * run the job, then remove it from the joblist |
|
116 | + * |
|
117 | + * @param JobList $jobList |
|
118 | + * @param ILogger|null $logger |
|
119 | + */ |
|
120 | + public function execute($jobList, ILogger $logger = null) { |
|
121 | + $target = $this->argument['url']; |
|
122 | + // only execute if target is still in the list of trusted domains |
|
123 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
124 | + $this->parentExecute($jobList, $logger); |
|
125 | + } |
|
126 | + |
|
127 | + $jobList->remove($this, $this->argument); |
|
128 | + |
|
129 | + if ($this->retainJob) { |
|
130 | + $this->reAddJob($this->argument); |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * call execute() method of parent |
|
136 | + * |
|
137 | + * @param JobList $jobList |
|
138 | + * @param ILogger $logger |
|
139 | + */ |
|
140 | + protected function parentExecute($jobList, $logger) { |
|
141 | + parent::execute($jobList, $logger); |
|
142 | + } |
|
143 | + |
|
144 | + protected function run($argument) { |
|
145 | + $target = $argument['url']; |
|
146 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
147 | + $currentTime = $this->timeFactory->getTime(); |
|
148 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
149 | + $source = rtrim($source, '/'); |
|
150 | + $token = $argument['token']; |
|
151 | + |
|
152 | + // kill job after 30 days of trying |
|
153 | + $deadline = $currentTime - $this->maxLifespan; |
|
154 | + if ($created < $deadline) { |
|
155 | + $this->retainJob = false; |
|
156 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
157 | + return; |
|
158 | + } |
|
159 | + |
|
160 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
161 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
162 | + |
|
163 | + // make sure that we have a well formated url |
|
164 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
165 | + |
|
166 | + try { |
|
167 | + $result = $this->httpClient->post( |
|
168 | + $url, |
|
169 | + [ |
|
170 | + 'body' => [ |
|
171 | + 'url' => $source, |
|
172 | + 'token' => $token, |
|
173 | + 'format' => 'json', |
|
174 | + ], |
|
175 | + 'timeout' => 3, |
|
176 | + 'connect_timeout' => 3, |
|
177 | + ] |
|
178 | + ); |
|
179 | + |
|
180 | + $status = $result->getStatusCode(); |
|
181 | + } catch (ClientException $e) { |
|
182 | + $status = $e->getCode(); |
|
183 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
184 | + $this->logger->info($target . ' refused to ask for a shared secret.', ['app' => 'federation']); |
|
185 | + } else { |
|
186 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
187 | + } |
|
188 | + } catch (RequestException $e) { |
|
189 | + $status = -1; // There is no status code if we could not connect |
|
190 | + $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
191 | + } catch (RingException $e) { |
|
192 | + $status = -1; // There is no status code if we could not connect |
|
193 | + $this->logger->info('Could not connect to ' . $target, ['app' => 'federation']); |
|
194 | + } catch (\Exception $e) { |
|
195 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
196 | + $this->logger->logException($e, ['app' => 'federation']); |
|
197 | + } |
|
198 | + |
|
199 | + // if we received a unexpected response we try again later |
|
200 | + if ( |
|
201 | + $status !== Http::STATUS_OK |
|
202 | + && $status !== Http::STATUS_FORBIDDEN |
|
203 | + ) { |
|
204 | + $this->retainJob = true; |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * re-add background job |
|
210 | + * |
|
211 | + * @param array $argument |
|
212 | + */ |
|
213 | + protected function reAddJob(array $argument) { |
|
214 | + $url = $argument['url']; |
|
215 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
216 | + $token = $argument['token']; |
|
217 | + |
|
218 | + $this->jobList->add( |
|
219 | + RequestSharedSecret::class, |
|
220 | + [ |
|
221 | + 'url' => $url, |
|
222 | + 'token' => $token, |
|
223 | + 'created' => $created |
|
224 | + ] |
|
225 | + ); |
|
226 | + } |
|
227 | 227 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | try { |
66 | 66 | return new \DateTimeZone($timeZone); |
67 | 67 | } catch (\Exception $e) { |
68 | - \OCP\Util::writeLog('datetimezone', 'Failed to created DateTimeZone "' . $timeZone . "'", ILogger::DEBUG); |
|
68 | + \OCP\Util::writeLog('datetimezone', 'Failed to created DateTimeZone "'.$timeZone."'", ILogger::DEBUG); |
|
69 | 69 | return new \DateTimeZone($this->getDefaultTimeZone()); |
70 | 70 | } |
71 | 71 | } |
@@ -86,9 +86,9 @@ discard block |
||
86 | 86 | // so a positive offset means negative timeZone |
87 | 87 | // and the other way around. |
88 | 88 | if ($offset > 0) { |
89 | - $timeZone = 'Etc/GMT-' . $offset; |
|
89 | + $timeZone = 'Etc/GMT-'.$offset; |
|
90 | 90 | } else { |
91 | - $timeZone = 'Etc/GMT+' . abs($offset); |
|
91 | + $timeZone = 'Etc/GMT+'.abs($offset); |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | return new \DateTimeZone($timeZone); |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | } |
111 | 111 | |
112 | 112 | // No timezone found, fallback to UTC |
113 | - \OCP\Util::writeLog('datetimezone', 'Failed to find DateTimeZone for offset "' . $offset . "'", ILogger::DEBUG); |
|
113 | + \OCP\Util::writeLog('datetimezone', 'Failed to find DateTimeZone for offset "'.$offset."'", ILogger::DEBUG); |
|
114 | 114 | return new \DateTimeZone($this->getDefaultTimeZone()); |
115 | 115 | } |
116 | 116 | } |
@@ -30,100 +30,100 @@ |
||
30 | 30 | use OCP\ISession; |
31 | 31 | |
32 | 32 | class DateTimeZone implements IDateTimeZone { |
33 | - /** @var IConfig */ |
|
34 | - protected $config; |
|
33 | + /** @var IConfig */ |
|
34 | + protected $config; |
|
35 | 35 | |
36 | - /** @var ISession */ |
|
37 | - protected $session; |
|
36 | + /** @var ISession */ |
|
37 | + protected $session; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Constructor |
|
41 | - * |
|
42 | - * @param IConfig $config |
|
43 | - * @param ISession $session |
|
44 | - */ |
|
45 | - public function __construct(IConfig $config, ISession $session) { |
|
46 | - $this->config = $config; |
|
47 | - $this->session = $session; |
|
48 | - } |
|
39 | + /** |
|
40 | + * Constructor |
|
41 | + * |
|
42 | + * @param IConfig $config |
|
43 | + * @param ISession $session |
|
44 | + */ |
|
45 | + public function __construct(IConfig $config, ISession $session) { |
|
46 | + $this->config = $config; |
|
47 | + $this->session = $session; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Get the timezone of the current user, based on his session information and config data |
|
52 | - * |
|
53 | - * @param bool|int $timestamp |
|
54 | - * @return \DateTimeZone |
|
55 | - */ |
|
56 | - public function getTimeZone($timestamp = false) { |
|
57 | - $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null); |
|
58 | - if ($timeZone === null) { |
|
59 | - if ($this->session->exists('timezone')) { |
|
60 | - return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp); |
|
61 | - } |
|
62 | - $timeZone = $this->getDefaultTimeZone(); |
|
63 | - } |
|
50 | + /** |
|
51 | + * Get the timezone of the current user, based on his session information and config data |
|
52 | + * |
|
53 | + * @param bool|int $timestamp |
|
54 | + * @return \DateTimeZone |
|
55 | + */ |
|
56 | + public function getTimeZone($timestamp = false) { |
|
57 | + $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null); |
|
58 | + if ($timeZone === null) { |
|
59 | + if ($this->session->exists('timezone')) { |
|
60 | + return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp); |
|
61 | + } |
|
62 | + $timeZone = $this->getDefaultTimeZone(); |
|
63 | + } |
|
64 | 64 | |
65 | - try { |
|
66 | - return new \DateTimeZone($timeZone); |
|
67 | - } catch (\Exception $e) { |
|
68 | - \OCP\Util::writeLog('datetimezone', 'Failed to created DateTimeZone "' . $timeZone . "'", ILogger::DEBUG); |
|
69 | - return new \DateTimeZone($this->getDefaultTimeZone()); |
|
70 | - } |
|
71 | - } |
|
65 | + try { |
|
66 | + return new \DateTimeZone($timeZone); |
|
67 | + } catch (\Exception $e) { |
|
68 | + \OCP\Util::writeLog('datetimezone', 'Failed to created DateTimeZone "' . $timeZone . "'", ILogger::DEBUG); |
|
69 | + return new \DateTimeZone($this->getDefaultTimeZone()); |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Guess the DateTimeZone for a given offset |
|
75 | - * |
|
76 | - * We first try to find a Etc/GMT* timezone, if that does not exist, |
|
77 | - * we try to find it manually, before falling back to UTC. |
|
78 | - * |
|
79 | - * @param mixed $offset |
|
80 | - * @param bool|int $timestamp |
|
81 | - * @return \DateTimeZone |
|
82 | - */ |
|
83 | - protected function guessTimeZoneFromOffset($offset, $timestamp) { |
|
84 | - try { |
|
85 | - // Note: the timeZone name is the inverse to the offset, |
|
86 | - // so a positive offset means negative timeZone |
|
87 | - // and the other way around. |
|
88 | - if ($offset > 0) { |
|
89 | - $timeZone = 'Etc/GMT-' . $offset; |
|
90 | - } else { |
|
91 | - $timeZone = 'Etc/GMT+' . abs($offset); |
|
92 | - } |
|
73 | + /** |
|
74 | + * Guess the DateTimeZone for a given offset |
|
75 | + * |
|
76 | + * We first try to find a Etc/GMT* timezone, if that does not exist, |
|
77 | + * we try to find it manually, before falling back to UTC. |
|
78 | + * |
|
79 | + * @param mixed $offset |
|
80 | + * @param bool|int $timestamp |
|
81 | + * @return \DateTimeZone |
|
82 | + */ |
|
83 | + protected function guessTimeZoneFromOffset($offset, $timestamp) { |
|
84 | + try { |
|
85 | + // Note: the timeZone name is the inverse to the offset, |
|
86 | + // so a positive offset means negative timeZone |
|
87 | + // and the other way around. |
|
88 | + if ($offset > 0) { |
|
89 | + $timeZone = 'Etc/GMT-' . $offset; |
|
90 | + } else { |
|
91 | + $timeZone = 'Etc/GMT+' . abs($offset); |
|
92 | + } |
|
93 | 93 | |
94 | - return new \DateTimeZone($timeZone); |
|
95 | - } catch (\Exception $e) { |
|
96 | - // If the offset has no Etc/GMT* timezone, |
|
97 | - // we try to guess one timezone that has the same offset |
|
98 | - foreach (\DateTimeZone::listIdentifiers() as $timeZone) { |
|
99 | - $dtz = new \DateTimeZone($timeZone); |
|
100 | - $dateTime = new \DateTime(); |
|
94 | + return new \DateTimeZone($timeZone); |
|
95 | + } catch (\Exception $e) { |
|
96 | + // If the offset has no Etc/GMT* timezone, |
|
97 | + // we try to guess one timezone that has the same offset |
|
98 | + foreach (\DateTimeZone::listIdentifiers() as $timeZone) { |
|
99 | + $dtz = new \DateTimeZone($timeZone); |
|
100 | + $dateTime = new \DateTime(); |
|
101 | 101 | |
102 | - if ($timestamp !== false) { |
|
103 | - $dateTime->setTimestamp($timestamp); |
|
104 | - } |
|
102 | + if ($timestamp !== false) { |
|
103 | + $dateTime->setTimestamp($timestamp); |
|
104 | + } |
|
105 | 105 | |
106 | - $dtOffset = $dtz->getOffset($dateTime); |
|
107 | - if ($dtOffset == 3600 * $offset) { |
|
108 | - return $dtz; |
|
109 | - } |
|
110 | - } |
|
106 | + $dtOffset = $dtz->getOffset($dateTime); |
|
107 | + if ($dtOffset == 3600 * $offset) { |
|
108 | + return $dtz; |
|
109 | + } |
|
110 | + } |
|
111 | 111 | |
112 | - // No timezone found, fallback to UTC |
|
113 | - \OCP\Util::writeLog('datetimezone', 'Failed to find DateTimeZone for offset "' . $offset . "'", ILogger::DEBUG); |
|
114 | - return new \DateTimeZone($this->getDefaultTimeZone()); |
|
115 | - } |
|
116 | - } |
|
112 | + // No timezone found, fallback to UTC |
|
113 | + \OCP\Util::writeLog('datetimezone', 'Failed to find DateTimeZone for offset "' . $offset . "'", ILogger::DEBUG); |
|
114 | + return new \DateTimeZone($this->getDefaultTimeZone()); |
|
115 | + } |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * Get the default timezone of the server |
|
120 | - * |
|
121 | - * Falls back to UTC if it is not yet set. |
|
122 | - * |
|
123 | - * @return string |
|
124 | - */ |
|
125 | - protected function getDefaultTimeZone() { |
|
126 | - $serverTimeZone = date_default_timezone_get(); |
|
127 | - return $serverTimeZone ?: 'UTC'; |
|
128 | - } |
|
118 | + /** |
|
119 | + * Get the default timezone of the server |
|
120 | + * |
|
121 | + * Falls back to UTC if it is not yet set. |
|
122 | + * |
|
123 | + * @return string |
|
124 | + */ |
|
125 | + protected function getDefaultTimeZone() { |
|
126 | + $serverTimeZone = date_default_timezone_get(); |
|
127 | + return $serverTimeZone ?: 'UTC'; |
|
128 | + } |
|
129 | 129 | } |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | } else { |
107 | 107 | // Update old classes to new namespace |
108 | 108 | if (strpos($storage, 'OC_Filestorage_') !== false) { |
109 | - $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
109 | + $storage = '\OC\Files\Storage\\'.substr($storage, 15); |
|
110 | 110 | } |
111 | 111 | $this->class = $storage; |
112 | 112 | $this->arguments = $arguments; |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | return; |
159 | 159 | } |
160 | 160 | } else { |
161 | - \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
161 | + \OCP\Util::writeLog('core', 'storage backend '.$this->class.' not found', ILogger::ERROR); |
|
162 | 162 | $this->invalidStorage = true; |
163 | 163 | return; |
164 | 164 | } |
@@ -208,13 +208,13 @@ discard block |
||
208 | 208 | */ |
209 | 209 | public function getInternalPath($path) { |
210 | 210 | $path = Filesystem::normalizePath($path, true, false, true); |
211 | - if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
211 | + if ($this->mountPoint === $path or $this->mountPoint.'/' === $path) { |
|
212 | 212 | $internalPath = ''; |
213 | 213 | } else { |
214 | 214 | $internalPath = substr($path, strlen($this->mountPoint)); |
215 | 215 | } |
216 | 216 | // substr returns false instead of an empty string, we always want a string |
217 | - return (string)$internalPath; |
|
217 | + return (string) $internalPath; |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | /** |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | */ |
268 | 268 | public function getStorageRootId() { |
269 | 269 | if (is_null($this->rootId)) { |
270 | - $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
270 | + $this->rootId = (int) $this->getStorage()->getCache()->getId(''); |
|
271 | 271 | } |
272 | 272 | return $this->rootId; |
273 | 273 | } |
@@ -37,247 +37,247 @@ |
||
37 | 37 | use OCP\ILogger; |
38 | 38 | |
39 | 39 | class MountPoint implements IMountPoint { |
40 | - /** |
|
41 | - * @var \OC\Files\Storage\Storage $storage |
|
42 | - */ |
|
43 | - protected $storage = null; |
|
44 | - protected $class; |
|
45 | - protected $storageId; |
|
46 | - protected $rootId = null; |
|
40 | + /** |
|
41 | + * @var \OC\Files\Storage\Storage $storage |
|
42 | + */ |
|
43 | + protected $storage = null; |
|
44 | + protected $class; |
|
45 | + protected $storageId; |
|
46 | + protected $rootId = null; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Configuration options for the storage backend |
|
50 | - * |
|
51 | - * @var array |
|
52 | - */ |
|
53 | - protected $arguments = []; |
|
54 | - protected $mountPoint; |
|
48 | + /** |
|
49 | + * Configuration options for the storage backend |
|
50 | + * |
|
51 | + * @var array |
|
52 | + */ |
|
53 | + protected $arguments = []; |
|
54 | + protected $mountPoint; |
|
55 | 55 | |
56 | - /** |
|
57 | - * Mount specific options |
|
58 | - * |
|
59 | - * @var array |
|
60 | - */ |
|
61 | - protected $mountOptions = []; |
|
56 | + /** |
|
57 | + * Mount specific options |
|
58 | + * |
|
59 | + * @var array |
|
60 | + */ |
|
61 | + protected $mountOptions = []; |
|
62 | 62 | |
63 | - /** |
|
64 | - * @var \OC\Files\Storage\StorageFactory $loader |
|
65 | - */ |
|
66 | - private $loader; |
|
63 | + /** |
|
64 | + * @var \OC\Files\Storage\StorageFactory $loader |
|
65 | + */ |
|
66 | + private $loader; |
|
67 | 67 | |
68 | - /** |
|
69 | - * Specified whether the storage is invalid after failing to |
|
70 | - * instantiate it. |
|
71 | - * |
|
72 | - * @var bool |
|
73 | - */ |
|
74 | - private $invalidStorage = false; |
|
68 | + /** |
|
69 | + * Specified whether the storage is invalid after failing to |
|
70 | + * instantiate it. |
|
71 | + * |
|
72 | + * @var bool |
|
73 | + */ |
|
74 | + private $invalidStorage = false; |
|
75 | 75 | |
76 | - /** @var int|null */ |
|
77 | - protected $mountId; |
|
76 | + /** @var int|null */ |
|
77 | + protected $mountId; |
|
78 | 78 | |
79 | - /** |
|
80 | - * @param string|\OC\Files\Storage\Storage $storage |
|
81 | - * @param string $mountpoint |
|
82 | - * @param array $arguments (optional) configuration for the storage backend |
|
83 | - * @param \OCP\Files\Storage\IStorageFactory $loader |
|
84 | - * @param array $mountOptions mount specific options |
|
85 | - * @param int|null $mountId |
|
86 | - * @throws \Exception |
|
87 | - */ |
|
88 | - public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) { |
|
89 | - if (is_null($arguments)) { |
|
90 | - $arguments = []; |
|
91 | - } |
|
92 | - if (is_null($loader)) { |
|
93 | - $this->loader = new StorageFactory(); |
|
94 | - } else { |
|
95 | - $this->loader = $loader; |
|
96 | - } |
|
79 | + /** |
|
80 | + * @param string|\OC\Files\Storage\Storage $storage |
|
81 | + * @param string $mountpoint |
|
82 | + * @param array $arguments (optional) configuration for the storage backend |
|
83 | + * @param \OCP\Files\Storage\IStorageFactory $loader |
|
84 | + * @param array $mountOptions mount specific options |
|
85 | + * @param int|null $mountId |
|
86 | + * @throws \Exception |
|
87 | + */ |
|
88 | + public function __construct($storage, $mountpoint, $arguments = null, $loader = null, $mountOptions = null, $mountId = null) { |
|
89 | + if (is_null($arguments)) { |
|
90 | + $arguments = []; |
|
91 | + } |
|
92 | + if (is_null($loader)) { |
|
93 | + $this->loader = new StorageFactory(); |
|
94 | + } else { |
|
95 | + $this->loader = $loader; |
|
96 | + } |
|
97 | 97 | |
98 | - if (!is_null($mountOptions)) { |
|
99 | - $this->mountOptions = $mountOptions; |
|
100 | - } |
|
98 | + if (!is_null($mountOptions)) { |
|
99 | + $this->mountOptions = $mountOptions; |
|
100 | + } |
|
101 | 101 | |
102 | - $mountpoint = $this->formatPath($mountpoint); |
|
103 | - $this->mountPoint = $mountpoint; |
|
104 | - if ($storage instanceof Storage) { |
|
105 | - $this->class = get_class($storage); |
|
106 | - $this->storage = $this->loader->wrap($this, $storage); |
|
107 | - } else { |
|
108 | - // Update old classes to new namespace |
|
109 | - if (strpos($storage, 'OC_Filestorage_') !== false) { |
|
110 | - $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
111 | - } |
|
112 | - $this->class = $storage; |
|
113 | - $this->arguments = $arguments; |
|
114 | - } |
|
115 | - $this->mountId = $mountId; |
|
116 | - } |
|
102 | + $mountpoint = $this->formatPath($mountpoint); |
|
103 | + $this->mountPoint = $mountpoint; |
|
104 | + if ($storage instanceof Storage) { |
|
105 | + $this->class = get_class($storage); |
|
106 | + $this->storage = $this->loader->wrap($this, $storage); |
|
107 | + } else { |
|
108 | + // Update old classes to new namespace |
|
109 | + if (strpos($storage, 'OC_Filestorage_') !== false) { |
|
110 | + $storage = '\OC\Files\Storage\\' . substr($storage, 15); |
|
111 | + } |
|
112 | + $this->class = $storage; |
|
113 | + $this->arguments = $arguments; |
|
114 | + } |
|
115 | + $this->mountId = $mountId; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * get complete path to the mount point, relative to data/ |
|
120 | - * |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public function getMountPoint() { |
|
124 | - return $this->mountPoint; |
|
125 | - } |
|
118 | + /** |
|
119 | + * get complete path to the mount point, relative to data/ |
|
120 | + * |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public function getMountPoint() { |
|
124 | + return $this->mountPoint; |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * Sets the mount point path, relative to data/ |
|
129 | - * |
|
130 | - * @param string $mountPoint new mount point |
|
131 | - */ |
|
132 | - public function setMountPoint($mountPoint) { |
|
133 | - $this->mountPoint = $this->formatPath($mountPoint); |
|
134 | - } |
|
127 | + /** |
|
128 | + * Sets the mount point path, relative to data/ |
|
129 | + * |
|
130 | + * @param string $mountPoint new mount point |
|
131 | + */ |
|
132 | + public function setMountPoint($mountPoint) { |
|
133 | + $this->mountPoint = $this->formatPath($mountPoint); |
|
134 | + } |
|
135 | 135 | |
136 | - /** |
|
137 | - * create the storage that is mounted |
|
138 | - */ |
|
139 | - private function createStorage() { |
|
140 | - if ($this->invalidStorage) { |
|
141 | - return; |
|
142 | - } |
|
136 | + /** |
|
137 | + * create the storage that is mounted |
|
138 | + */ |
|
139 | + private function createStorage() { |
|
140 | + if ($this->invalidStorage) { |
|
141 | + return; |
|
142 | + } |
|
143 | 143 | |
144 | - if (class_exists($this->class)) { |
|
145 | - try { |
|
146 | - $class = $this->class; |
|
147 | - // prevent recursion by setting the storage before applying wrappers |
|
148 | - $this->storage = new $class($this->arguments); |
|
149 | - $this->storage = $this->loader->wrap($this, $this->storage); |
|
150 | - } catch (\Exception $exception) { |
|
151 | - $this->storage = null; |
|
152 | - $this->invalidStorage = true; |
|
153 | - if ($this->mountPoint === '/') { |
|
154 | - // the root storage could not be initialized, show the user! |
|
155 | - throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception); |
|
156 | - } else { |
|
157 | - \OC::$server->getLogger()->logException($exception, ['level' => ILogger::ERROR]); |
|
158 | - } |
|
159 | - return; |
|
160 | - } |
|
161 | - } else { |
|
162 | - \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
163 | - $this->invalidStorage = true; |
|
164 | - return; |
|
165 | - } |
|
166 | - } |
|
144 | + if (class_exists($this->class)) { |
|
145 | + try { |
|
146 | + $class = $this->class; |
|
147 | + // prevent recursion by setting the storage before applying wrappers |
|
148 | + $this->storage = new $class($this->arguments); |
|
149 | + $this->storage = $this->loader->wrap($this, $this->storage); |
|
150 | + } catch (\Exception $exception) { |
|
151 | + $this->storage = null; |
|
152 | + $this->invalidStorage = true; |
|
153 | + if ($this->mountPoint === '/') { |
|
154 | + // the root storage could not be initialized, show the user! |
|
155 | + throw new \Exception('The root storage could not be initialized. Please contact your local administrator.', $exception->getCode(), $exception); |
|
156 | + } else { |
|
157 | + \OC::$server->getLogger()->logException($exception, ['level' => ILogger::ERROR]); |
|
158 | + } |
|
159 | + return; |
|
160 | + } |
|
161 | + } else { |
|
162 | + \OCP\Util::writeLog('core', 'storage backend ' . $this->class . ' not found', ILogger::ERROR); |
|
163 | + $this->invalidStorage = true; |
|
164 | + return; |
|
165 | + } |
|
166 | + } |
|
167 | 167 | |
168 | - /** |
|
169 | - * @return \OC\Files\Storage\Storage |
|
170 | - */ |
|
171 | - public function getStorage() { |
|
172 | - if (is_null($this->storage)) { |
|
173 | - $this->createStorage(); |
|
174 | - } |
|
175 | - return $this->storage; |
|
176 | - } |
|
168 | + /** |
|
169 | + * @return \OC\Files\Storage\Storage |
|
170 | + */ |
|
171 | + public function getStorage() { |
|
172 | + if (is_null($this->storage)) { |
|
173 | + $this->createStorage(); |
|
174 | + } |
|
175 | + return $this->storage; |
|
176 | + } |
|
177 | 177 | |
178 | - /** |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public function getStorageId() { |
|
182 | - if (!$this->storageId) { |
|
183 | - if (is_null($this->storage)) { |
|
184 | - $storage = $this->createStorage(); //FIXME: start using exceptions |
|
185 | - if (is_null($storage)) { |
|
186 | - return null; |
|
187 | - } |
|
178 | + /** |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public function getStorageId() { |
|
182 | + if (!$this->storageId) { |
|
183 | + if (is_null($this->storage)) { |
|
184 | + $storage = $this->createStorage(); //FIXME: start using exceptions |
|
185 | + if (is_null($storage)) { |
|
186 | + return null; |
|
187 | + } |
|
188 | 188 | |
189 | - $this->storage = $storage; |
|
190 | - } |
|
191 | - $this->storageId = $this->storage->getId(); |
|
192 | - if (strlen($this->storageId) > 64) { |
|
193 | - $this->storageId = md5($this->storageId); |
|
194 | - } |
|
195 | - } |
|
196 | - return $this->storageId; |
|
197 | - } |
|
189 | + $this->storage = $storage; |
|
190 | + } |
|
191 | + $this->storageId = $this->storage->getId(); |
|
192 | + if (strlen($this->storageId) > 64) { |
|
193 | + $this->storageId = md5($this->storageId); |
|
194 | + } |
|
195 | + } |
|
196 | + return $this->storageId; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * @return int |
|
201 | - */ |
|
202 | - public function getNumericStorageId() { |
|
203 | - return $this->getStorage()->getStorageCache()->getNumericId(); |
|
204 | - } |
|
199 | + /** |
|
200 | + * @return int |
|
201 | + */ |
|
202 | + public function getNumericStorageId() { |
|
203 | + return $this->getStorage()->getStorageCache()->getNumericId(); |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * @param string $path |
|
208 | - * @return string |
|
209 | - */ |
|
210 | - public function getInternalPath($path) { |
|
211 | - $path = Filesystem::normalizePath($path, true, false, true); |
|
212 | - if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
213 | - $internalPath = ''; |
|
214 | - } else { |
|
215 | - $internalPath = substr($path, strlen($this->mountPoint)); |
|
216 | - } |
|
217 | - // substr returns false instead of an empty string, we always want a string |
|
218 | - return (string)$internalPath; |
|
219 | - } |
|
206 | + /** |
|
207 | + * @param string $path |
|
208 | + * @return string |
|
209 | + */ |
|
210 | + public function getInternalPath($path) { |
|
211 | + $path = Filesystem::normalizePath($path, true, false, true); |
|
212 | + if ($this->mountPoint === $path or $this->mountPoint . '/' === $path) { |
|
213 | + $internalPath = ''; |
|
214 | + } else { |
|
215 | + $internalPath = substr($path, strlen($this->mountPoint)); |
|
216 | + } |
|
217 | + // substr returns false instead of an empty string, we always want a string |
|
218 | + return (string)$internalPath; |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * @param string $path |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - private function formatPath($path) { |
|
226 | - $path = Filesystem::normalizePath($path); |
|
227 | - if (strlen($path) > 1) { |
|
228 | - $path .= '/'; |
|
229 | - } |
|
230 | - return $path; |
|
231 | - } |
|
221 | + /** |
|
222 | + * @param string $path |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + private function formatPath($path) { |
|
226 | + $path = Filesystem::normalizePath($path); |
|
227 | + if (strlen($path) > 1) { |
|
228 | + $path .= '/'; |
|
229 | + } |
|
230 | + return $path; |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * @param callable $wrapper |
|
235 | - */ |
|
236 | - public function wrapStorage($wrapper) { |
|
237 | - $storage = $this->getStorage(); |
|
238 | - // storage can be null if it couldn't be initialized |
|
239 | - if ($storage != null) { |
|
240 | - $this->storage = $wrapper($this->mountPoint, $storage, $this); |
|
241 | - } |
|
242 | - } |
|
233 | + /** |
|
234 | + * @param callable $wrapper |
|
235 | + */ |
|
236 | + public function wrapStorage($wrapper) { |
|
237 | + $storage = $this->getStorage(); |
|
238 | + // storage can be null if it couldn't be initialized |
|
239 | + if ($storage != null) { |
|
240 | + $this->storage = $wrapper($this->mountPoint, $storage, $this); |
|
241 | + } |
|
242 | + } |
|
243 | 243 | |
244 | - /** |
|
245 | - * Get a mount option |
|
246 | - * |
|
247 | - * @param string $name Name of the mount option to get |
|
248 | - * @param mixed $default Default value for the mount option |
|
249 | - * @return mixed |
|
250 | - */ |
|
251 | - public function getOption($name, $default) { |
|
252 | - return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
253 | - } |
|
244 | + /** |
|
245 | + * Get a mount option |
|
246 | + * |
|
247 | + * @param string $name Name of the mount option to get |
|
248 | + * @param mixed $default Default value for the mount option |
|
249 | + * @return mixed |
|
250 | + */ |
|
251 | + public function getOption($name, $default) { |
|
252 | + return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; |
|
253 | + } |
|
254 | 254 | |
255 | - /** |
|
256 | - * Get all options for the mount |
|
257 | - * |
|
258 | - * @return array |
|
259 | - */ |
|
260 | - public function getOptions() { |
|
261 | - return $this->mountOptions; |
|
262 | - } |
|
255 | + /** |
|
256 | + * Get all options for the mount |
|
257 | + * |
|
258 | + * @return array |
|
259 | + */ |
|
260 | + public function getOptions() { |
|
261 | + return $this->mountOptions; |
|
262 | + } |
|
263 | 263 | |
264 | - /** |
|
265 | - * Get the file id of the root of the storage |
|
266 | - * |
|
267 | - * @return int |
|
268 | - */ |
|
269 | - public function getStorageRootId() { |
|
270 | - if (is_null($this->rootId)) { |
|
271 | - $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
272 | - } |
|
273 | - return $this->rootId; |
|
274 | - } |
|
264 | + /** |
|
265 | + * Get the file id of the root of the storage |
|
266 | + * |
|
267 | + * @return int |
|
268 | + */ |
|
269 | + public function getStorageRootId() { |
|
270 | + if (is_null($this->rootId)) { |
|
271 | + $this->rootId = (int)$this->getStorage()->getCache()->getId(''); |
|
272 | + } |
|
273 | + return $this->rootId; |
|
274 | + } |
|
275 | 275 | |
276 | - public function getMountId() { |
|
277 | - return $this->mountId; |
|
278 | - } |
|
276 | + public function getMountId() { |
|
277 | + return $this->mountId; |
|
278 | + } |
|
279 | 279 | |
280 | - public function getMountType() { |
|
281 | - return ''; |
|
282 | - } |
|
280 | + public function getMountType() { |
|
281 | + return ''; |
|
282 | + } |
|
283 | 283 | } |
@@ -31,13 +31,13 @@ |
||
31 | 31 | * @since 14.0.0 |
32 | 32 | */ |
33 | 33 | interface IFileBased { |
34 | - /** |
|
35 | - * @since 14.0.0 |
|
36 | - */ |
|
37 | - public function getLogFilePath():string; |
|
34 | + /** |
|
35 | + * @since 14.0.0 |
|
36 | + */ |
|
37 | + public function getLogFilePath():string; |
|
38 | 38 | |
39 | - /** |
|
40 | - * @since 14.0.0 |
|
41 | - */ |
|
42 | - public function getEntries(int $limit=50, int $offset=0): array; |
|
39 | + /** |
|
40 | + * @since 14.0.0 |
|
41 | + */ |
|
42 | + public function getEntries(int $limit=50, int $offset=0): array; |
|
43 | 43 | } |
@@ -39,5 +39,5 @@ |
||
39 | 39 | /** |
40 | 40 | * @since 14.0.0 |
41 | 41 | */ |
42 | - public function getEntries(int $limit=50, int $offset=0): array; |
|
42 | + public function getEntries(int $limit = 50, int $offset = 0): array; |
|
43 | 43 | } |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | |
146 | 146 | protected function run($argument) { |
147 | 147 | $target = $argument['url']; |
148 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
148 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->timeFactory->getTime(); |
|
149 | 149 | $currentTime = $this->timeFactory->getTime(); |
150 | 150 | $source = $this->urlGenerator->getAbsoluteURL('/'); |
151 | 151 | $source = rtrim($source, '/'); |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | $deadline = $currentTime - $this->maxLifespan; |
156 | 156 | if ($created < $deadline) { |
157 | 157 | $this->retainJob = false; |
158 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
158 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
159 | 159 | return; |
160 | 160 | } |
161 | 161 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
164 | 164 | |
165 | 165 | // make sure that we have a well formatted url |
166 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
166 | + $url = rtrim($target, '/').'/'.trim($endPoint, '/'); |
|
167 | 167 | |
168 | 168 | $result = null; |
169 | 169 | try { |
@@ -186,21 +186,21 @@ discard block |
||
186 | 186 | } catch (ClientException $e) { |
187 | 187 | $status = $e->getCode(); |
188 | 188 | if ($status === Http::STATUS_FORBIDDEN) { |
189 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
189 | + $this->logger->info($target.' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
190 | 190 | } else { |
191 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
191 | + $this->logger->info($target.' responded with a '.$status.' containing: '.$e->getMessage(), ['app' => 'federation']); |
|
192 | 192 | } |
193 | 193 | } catch (RequestException $e) { |
194 | 194 | $status = -1; // There is no status code if we could not connect |
195 | 195 | $this->logger->logException($e, [ |
196 | - 'message' => 'Could not connect to ' . $target, |
|
196 | + 'message' => 'Could not connect to '.$target, |
|
197 | 197 | 'level' => ILogger::INFO, |
198 | 198 | 'app' => 'federation', |
199 | 199 | ]); |
200 | 200 | } catch (RingException $e) { |
201 | 201 | $status = -1; // There is no status code if we could not connect |
202 | 202 | $this->logger->logException($e, [ |
203 | - 'message' => 'Could not connect to ' . $target, |
|
203 | + 'message' => 'Could not connect to '.$target, |
|
204 | 204 | 'level' => ILogger::INFO, |
205 | 205 | 'app' => 'federation', |
206 | 206 | ]); |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | ); |
228 | 228 | } else { |
229 | 229 | $this->logger->error( |
230 | - 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
230 | + 'remote server "'.$target.'"" does not return a valid shared secret. Received data: '.$body, |
|
231 | 231 | ['app' => 'federation'] |
232 | 232 | ); |
233 | 233 | $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | */ |
244 | 244 | protected function reAddJob(array $argument) { |
245 | 245 | $url = $argument['url']; |
246 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
246 | + $created = isset($argument['created']) ? (int) $argument['created'] : $this->timeFactory->getTime(); |
|
247 | 247 | $token = $argument['token']; |
248 | 248 | $this->jobList->add( |
249 | 249 | GetSharedSecret::class, |
@@ -54,201 +54,201 @@ |
||
54 | 54 | */ |
55 | 55 | class GetSharedSecret extends Job { |
56 | 56 | |
57 | - /** @var IClient */ |
|
58 | - private $httpClient; |
|
59 | - |
|
60 | - /** @var IJobList */ |
|
61 | - private $jobList; |
|
62 | - |
|
63 | - /** @var IURLGenerator */ |
|
64 | - private $urlGenerator; |
|
65 | - |
|
66 | - /** @var TrustedServers */ |
|
67 | - private $trustedServers; |
|
68 | - |
|
69 | - /** @var IDiscoveryService */ |
|
70 | - private $ocsDiscoveryService; |
|
71 | - |
|
72 | - /** @var ILogger */ |
|
73 | - private $logger; |
|
74 | - |
|
75 | - /** @var ITimeFactory */ |
|
76 | - private $timeFactory; |
|
77 | - |
|
78 | - /** @var bool */ |
|
79 | - protected $retainJob = false; |
|
80 | - |
|
81 | - private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
82 | - |
|
83 | - /** @var int 30 day = 2592000sec */ |
|
84 | - private $maxLifespan = 2592000; |
|
85 | - |
|
86 | - /** |
|
87 | - * RequestSharedSecret constructor. |
|
88 | - * |
|
89 | - * @param IClientService $httpClientService |
|
90 | - * @param IURLGenerator $urlGenerator |
|
91 | - * @param IJobList $jobList |
|
92 | - * @param TrustedServers $trustedServers |
|
93 | - * @param ILogger $logger |
|
94 | - * @param IDiscoveryService $ocsDiscoveryService |
|
95 | - * @param ITimeFactory $timeFactory |
|
96 | - */ |
|
97 | - public function __construct( |
|
98 | - IClientService $httpClientService, |
|
99 | - IURLGenerator $urlGenerator, |
|
100 | - IJobList $jobList, |
|
101 | - TrustedServers $trustedServers, |
|
102 | - ILogger $logger, |
|
103 | - IDiscoveryService $ocsDiscoveryService, |
|
104 | - ITimeFactory $timeFactory |
|
105 | - ) { |
|
106 | - $this->logger = $logger; |
|
107 | - $this->httpClient = $httpClientService->newClient(); |
|
108 | - $this->jobList = $jobList; |
|
109 | - $this->urlGenerator = $urlGenerator; |
|
110 | - $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
111 | - $this->trustedServers = $trustedServers; |
|
112 | - $this->timeFactory = $timeFactory; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * run the job, then remove it from the joblist |
|
117 | - * |
|
118 | - * @param JobList $jobList |
|
119 | - * @param ILogger|null $logger |
|
120 | - */ |
|
121 | - public function execute($jobList, ILogger $logger = null) { |
|
122 | - $target = $this->argument['url']; |
|
123 | - // only execute if target is still in the list of trusted domains |
|
124 | - if ($this->trustedServers->isTrustedServer($target)) { |
|
125 | - $this->parentExecute($jobList, $logger); |
|
126 | - } |
|
127 | - |
|
128 | - $jobList->remove($this, $this->argument); |
|
129 | - |
|
130 | - if ($this->retainJob) { |
|
131 | - $this->reAddJob($this->argument); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * call execute() method of parent |
|
137 | - * |
|
138 | - * @param JobList $jobList |
|
139 | - * @param ILogger $logger |
|
140 | - */ |
|
141 | - protected function parentExecute($jobList, $logger = null) { |
|
142 | - parent::execute($jobList, $logger); |
|
143 | - } |
|
144 | - |
|
145 | - protected function run($argument) { |
|
146 | - $target = $argument['url']; |
|
147 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
148 | - $currentTime = $this->timeFactory->getTime(); |
|
149 | - $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
150 | - $source = rtrim($source, '/'); |
|
151 | - $token = $argument['token']; |
|
152 | - |
|
153 | - // kill job after 30 days of trying |
|
154 | - $deadline = $currentTime - $this->maxLifespan; |
|
155 | - if ($created < $deadline) { |
|
156 | - $this->retainJob = false; |
|
157 | - $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
158 | - return; |
|
159 | - } |
|
160 | - |
|
161 | - $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
162 | - $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
163 | - |
|
164 | - // make sure that we have a well formatted url |
|
165 | - $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
166 | - |
|
167 | - $result = null; |
|
168 | - try { |
|
169 | - $result = $this->httpClient->get( |
|
170 | - $url, |
|
171 | - [ |
|
172 | - 'query' => |
|
173 | - [ |
|
174 | - 'url' => $source, |
|
175 | - 'token' => $token, |
|
176 | - 'format' => 'json', |
|
177 | - ], |
|
178 | - 'timeout' => 3, |
|
179 | - 'connect_timeout' => 3, |
|
180 | - ] |
|
181 | - ); |
|
182 | - |
|
183 | - $status = $result->getStatusCode(); |
|
184 | - } catch (ClientException $e) { |
|
185 | - $status = $e->getCode(); |
|
186 | - if ($status === Http::STATUS_FORBIDDEN) { |
|
187 | - $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
188 | - } else { |
|
189 | - $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
190 | - } |
|
191 | - } catch (RequestException $e) { |
|
192 | - $status = -1; // There is no status code if we could not connect |
|
193 | - $this->logger->logException($e, [ |
|
194 | - 'message' => 'Could not connect to ' . $target, |
|
195 | - 'level' => ILogger::INFO, |
|
196 | - 'app' => 'federation', |
|
197 | - ]); |
|
198 | - } catch (RingException $e) { |
|
199 | - $status = -1; // There is no status code if we could not connect |
|
200 | - $this->logger->logException($e, [ |
|
201 | - 'message' => 'Could not connect to ' . $target, |
|
202 | - 'level' => ILogger::INFO, |
|
203 | - 'app' => 'federation', |
|
204 | - ]); |
|
205 | - } catch (\Exception $e) { |
|
206 | - $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
207 | - $this->logger->logException($e, ['app' => 'federation']); |
|
208 | - } |
|
209 | - |
|
210 | - // if we received a unexpected response we try again later |
|
211 | - if ( |
|
212 | - $status !== Http::STATUS_OK |
|
213 | - && $status !== Http::STATUS_FORBIDDEN |
|
214 | - ) { |
|
215 | - $this->retainJob = true; |
|
216 | - } |
|
217 | - |
|
218 | - if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
219 | - $body = $result->getBody(); |
|
220 | - $result = json_decode($body, true); |
|
221 | - if (isset($result['ocs']['data']['sharedSecret'])) { |
|
222 | - $this->trustedServers->addSharedSecret( |
|
223 | - $target, |
|
224 | - $result['ocs']['data']['sharedSecret'] |
|
225 | - ); |
|
226 | - } else { |
|
227 | - $this->logger->error( |
|
228 | - 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
229 | - ['app' => 'federation'] |
|
230 | - ); |
|
231 | - $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
232 | - } |
|
233 | - } |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * re-add background job |
|
238 | - * |
|
239 | - * @param array $argument |
|
240 | - */ |
|
241 | - protected function reAddJob(array $argument) { |
|
242 | - $url = $argument['url']; |
|
243 | - $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
244 | - $token = $argument['token']; |
|
245 | - $this->jobList->add( |
|
246 | - GetSharedSecret::class, |
|
247 | - [ |
|
248 | - 'url' => $url, |
|
249 | - 'token' => $token, |
|
250 | - 'created' => $created |
|
251 | - ] |
|
252 | - ); |
|
253 | - } |
|
57 | + /** @var IClient */ |
|
58 | + private $httpClient; |
|
59 | + |
|
60 | + /** @var IJobList */ |
|
61 | + private $jobList; |
|
62 | + |
|
63 | + /** @var IURLGenerator */ |
|
64 | + private $urlGenerator; |
|
65 | + |
|
66 | + /** @var TrustedServers */ |
|
67 | + private $trustedServers; |
|
68 | + |
|
69 | + /** @var IDiscoveryService */ |
|
70 | + private $ocsDiscoveryService; |
|
71 | + |
|
72 | + /** @var ILogger */ |
|
73 | + private $logger; |
|
74 | + |
|
75 | + /** @var ITimeFactory */ |
|
76 | + private $timeFactory; |
|
77 | + |
|
78 | + /** @var bool */ |
|
79 | + protected $retainJob = false; |
|
80 | + |
|
81 | + private $defaultEndPoint = '/ocs/v2.php/apps/federation/api/v1/shared-secret'; |
|
82 | + |
|
83 | + /** @var int 30 day = 2592000sec */ |
|
84 | + private $maxLifespan = 2592000; |
|
85 | + |
|
86 | + /** |
|
87 | + * RequestSharedSecret constructor. |
|
88 | + * |
|
89 | + * @param IClientService $httpClientService |
|
90 | + * @param IURLGenerator $urlGenerator |
|
91 | + * @param IJobList $jobList |
|
92 | + * @param TrustedServers $trustedServers |
|
93 | + * @param ILogger $logger |
|
94 | + * @param IDiscoveryService $ocsDiscoveryService |
|
95 | + * @param ITimeFactory $timeFactory |
|
96 | + */ |
|
97 | + public function __construct( |
|
98 | + IClientService $httpClientService, |
|
99 | + IURLGenerator $urlGenerator, |
|
100 | + IJobList $jobList, |
|
101 | + TrustedServers $trustedServers, |
|
102 | + ILogger $logger, |
|
103 | + IDiscoveryService $ocsDiscoveryService, |
|
104 | + ITimeFactory $timeFactory |
|
105 | + ) { |
|
106 | + $this->logger = $logger; |
|
107 | + $this->httpClient = $httpClientService->newClient(); |
|
108 | + $this->jobList = $jobList; |
|
109 | + $this->urlGenerator = $urlGenerator; |
|
110 | + $this->ocsDiscoveryService = $ocsDiscoveryService; |
|
111 | + $this->trustedServers = $trustedServers; |
|
112 | + $this->timeFactory = $timeFactory; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * run the job, then remove it from the joblist |
|
117 | + * |
|
118 | + * @param JobList $jobList |
|
119 | + * @param ILogger|null $logger |
|
120 | + */ |
|
121 | + public function execute($jobList, ILogger $logger = null) { |
|
122 | + $target = $this->argument['url']; |
|
123 | + // only execute if target is still in the list of trusted domains |
|
124 | + if ($this->trustedServers->isTrustedServer($target)) { |
|
125 | + $this->parentExecute($jobList, $logger); |
|
126 | + } |
|
127 | + |
|
128 | + $jobList->remove($this, $this->argument); |
|
129 | + |
|
130 | + if ($this->retainJob) { |
|
131 | + $this->reAddJob($this->argument); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * call execute() method of parent |
|
137 | + * |
|
138 | + * @param JobList $jobList |
|
139 | + * @param ILogger $logger |
|
140 | + */ |
|
141 | + protected function parentExecute($jobList, $logger = null) { |
|
142 | + parent::execute($jobList, $logger); |
|
143 | + } |
|
144 | + |
|
145 | + protected function run($argument) { |
|
146 | + $target = $argument['url']; |
|
147 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
148 | + $currentTime = $this->timeFactory->getTime(); |
|
149 | + $source = $this->urlGenerator->getAbsoluteURL('/'); |
|
150 | + $source = rtrim($source, '/'); |
|
151 | + $token = $argument['token']; |
|
152 | + |
|
153 | + // kill job after 30 days of trying |
|
154 | + $deadline = $currentTime - $this->maxLifespan; |
|
155 | + if ($created < $deadline) { |
|
156 | + $this->retainJob = false; |
|
157 | + $this->trustedServers->setServerStatus($target,TrustedServers::STATUS_FAILURE); |
|
158 | + return; |
|
159 | + } |
|
160 | + |
|
161 | + $endPoints = $this->ocsDiscoveryService->discover($target, 'FEDERATED_SHARING'); |
|
162 | + $endPoint = isset($endPoints['shared-secret']) ? $endPoints['shared-secret'] : $this->defaultEndPoint; |
|
163 | + |
|
164 | + // make sure that we have a well formatted url |
|
165 | + $url = rtrim($target, '/') . '/' . trim($endPoint, '/'); |
|
166 | + |
|
167 | + $result = null; |
|
168 | + try { |
|
169 | + $result = $this->httpClient->get( |
|
170 | + $url, |
|
171 | + [ |
|
172 | + 'query' => |
|
173 | + [ |
|
174 | + 'url' => $source, |
|
175 | + 'token' => $token, |
|
176 | + 'format' => 'json', |
|
177 | + ], |
|
178 | + 'timeout' => 3, |
|
179 | + 'connect_timeout' => 3, |
|
180 | + ] |
|
181 | + ); |
|
182 | + |
|
183 | + $status = $result->getStatusCode(); |
|
184 | + } catch (ClientException $e) { |
|
185 | + $status = $e->getCode(); |
|
186 | + if ($status === Http::STATUS_FORBIDDEN) { |
|
187 | + $this->logger->info($target . ' refused to exchange a shared secret with you.', ['app' => 'federation']); |
|
188 | + } else { |
|
189 | + $this->logger->info($target . ' responded with a ' . $status . ' containing: ' . $e->getMessage(), ['app' => 'federation']); |
|
190 | + } |
|
191 | + } catch (RequestException $e) { |
|
192 | + $status = -1; // There is no status code if we could not connect |
|
193 | + $this->logger->logException($e, [ |
|
194 | + 'message' => 'Could not connect to ' . $target, |
|
195 | + 'level' => ILogger::INFO, |
|
196 | + 'app' => 'federation', |
|
197 | + ]); |
|
198 | + } catch (RingException $e) { |
|
199 | + $status = -1; // There is no status code if we could not connect |
|
200 | + $this->logger->logException($e, [ |
|
201 | + 'message' => 'Could not connect to ' . $target, |
|
202 | + 'level' => ILogger::INFO, |
|
203 | + 'app' => 'federation', |
|
204 | + ]); |
|
205 | + } catch (\Exception $e) { |
|
206 | + $status = Http::STATUS_INTERNAL_SERVER_ERROR; |
|
207 | + $this->logger->logException($e, ['app' => 'federation']); |
|
208 | + } |
|
209 | + |
|
210 | + // if we received a unexpected response we try again later |
|
211 | + if ( |
|
212 | + $status !== Http::STATUS_OK |
|
213 | + && $status !== Http::STATUS_FORBIDDEN |
|
214 | + ) { |
|
215 | + $this->retainJob = true; |
|
216 | + } |
|
217 | + |
|
218 | + if ($status === Http::STATUS_OK && $result instanceof IResponse) { |
|
219 | + $body = $result->getBody(); |
|
220 | + $result = json_decode($body, true); |
|
221 | + if (isset($result['ocs']['data']['sharedSecret'])) { |
|
222 | + $this->trustedServers->addSharedSecret( |
|
223 | + $target, |
|
224 | + $result['ocs']['data']['sharedSecret'] |
|
225 | + ); |
|
226 | + } else { |
|
227 | + $this->logger->error( |
|
228 | + 'remote server "' . $target . '"" does not return a valid shared secret. Received data: ' . $body, |
|
229 | + ['app' => 'federation'] |
|
230 | + ); |
|
231 | + $this->trustedServers->setServerStatus($target, TrustedServers::STATUS_FAILURE); |
|
232 | + } |
|
233 | + } |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * re-add background job |
|
238 | + * |
|
239 | + * @param array $argument |
|
240 | + */ |
|
241 | + protected function reAddJob(array $argument) { |
|
242 | + $url = $argument['url']; |
|
243 | + $created = isset($argument['created']) ? (int)$argument['created'] : $this->timeFactory->getTime(); |
|
244 | + $token = $argument['token']; |
|
245 | + $this->jobList->add( |
|
246 | + GetSharedSecret::class, |
|
247 | + [ |
|
248 | + 'url' => $url, |
|
249 | + 'token' => $token, |
|
250 | + 'created' => $created |
|
251 | + ] |
|
252 | + ); |
|
253 | + } |
|
254 | 254 | } |