@@ -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 | |
@@ -56,325 +56,325 @@ |
||
| 56 | 56 | */ |
| 57 | 57 | class Log implements ILogger, IDataLogger { |
| 58 | 58 | |
| 59 | - /** @var IWriter */ |
|
| 60 | - private $logger; |
|
| 61 | - |
|
| 62 | - /** @var SystemConfig */ |
|
| 63 | - private $config; |
|
| 64 | - |
|
| 65 | - /** @var boolean|null cache the result of the log condition check for the request */ |
|
| 66 | - private $logConditionSatisfied = null; |
|
| 67 | - |
|
| 68 | - /** @var Normalizer */ |
|
| 69 | - private $normalizer; |
|
| 70 | - |
|
| 71 | - /** @var IRegistry */ |
|
| 72 | - private $crashReporters; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param IWriter $logger The logger that should be used |
|
| 76 | - * @param SystemConfig $config the system config object |
|
| 77 | - * @param Normalizer|null $normalizer |
|
| 78 | - * @param IRegistry|null $registry |
|
| 79 | - */ |
|
| 80 | - public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
| 81 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
| 82 | - if ($config === null) { |
|
| 83 | - $config = \OC::$server->getSystemConfig(); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $this->config = $config; |
|
| 87 | - $this->logger = $logger; |
|
| 88 | - if ($normalizer === null) { |
|
| 89 | - $this->normalizer = new Normalizer(); |
|
| 90 | - } else { |
|
| 91 | - $this->normalizer = $normalizer; |
|
| 92 | - } |
|
| 93 | - $this->crashReporters = $registry; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * System is unusable. |
|
| 98 | - * |
|
| 99 | - * @param string $message |
|
| 100 | - * @param array $context |
|
| 101 | - * @return void |
|
| 102 | - */ |
|
| 103 | - public function emergency(string $message, array $context = []) { |
|
| 104 | - $this->log(ILogger::FATAL, $message, $context); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Action must be taken immediately. |
|
| 109 | - * |
|
| 110 | - * Example: Entire website down, database unavailable, etc. This should |
|
| 111 | - * trigger the SMS alerts and wake you up. |
|
| 112 | - * |
|
| 113 | - * @param string $message |
|
| 114 | - * @param array $context |
|
| 115 | - * @return void |
|
| 116 | - */ |
|
| 117 | - public function alert(string $message, array $context = []) { |
|
| 118 | - $this->log(ILogger::ERROR, $message, $context); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Critical conditions. |
|
| 123 | - * |
|
| 124 | - * Example: Application component unavailable, unexpected exception. |
|
| 125 | - * |
|
| 126 | - * @param string $message |
|
| 127 | - * @param array $context |
|
| 128 | - * @return void |
|
| 129 | - */ |
|
| 130 | - public function critical(string $message, array $context = []) { |
|
| 131 | - $this->log(ILogger::ERROR, $message, $context); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Runtime errors that do not require immediate action but should typically |
|
| 136 | - * be logged and monitored. |
|
| 137 | - * |
|
| 138 | - * @param string $message |
|
| 139 | - * @param array $context |
|
| 140 | - * @return void |
|
| 141 | - */ |
|
| 142 | - public function error(string $message, array $context = []) { |
|
| 143 | - $this->log(ILogger::ERROR, $message, $context); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Exceptional occurrences that are not errors. |
|
| 148 | - * |
|
| 149 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
| 150 | - * that are not necessarily wrong. |
|
| 151 | - * |
|
| 152 | - * @param string $message |
|
| 153 | - * @param array $context |
|
| 154 | - * @return void |
|
| 155 | - */ |
|
| 156 | - public function warning(string $message, array $context = []) { |
|
| 157 | - $this->log(ILogger::WARN, $message, $context); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Normal but significant events. |
|
| 162 | - * |
|
| 163 | - * @param string $message |
|
| 164 | - * @param array $context |
|
| 165 | - * @return void |
|
| 166 | - */ |
|
| 167 | - public function notice(string $message, array $context = []) { |
|
| 168 | - $this->log(ILogger::INFO, $message, $context); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Interesting events. |
|
| 173 | - * |
|
| 174 | - * Example: User logs in, SQL logs. |
|
| 175 | - * |
|
| 176 | - * @param string $message |
|
| 177 | - * @param array $context |
|
| 178 | - * @return void |
|
| 179 | - */ |
|
| 180 | - public function info(string $message, array $context = []) { |
|
| 181 | - $this->log(ILogger::INFO, $message, $context); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Detailed debug information. |
|
| 186 | - * |
|
| 187 | - * @param string $message |
|
| 188 | - * @param array $context |
|
| 189 | - * @return void |
|
| 190 | - */ |
|
| 191 | - public function debug(string $message, array $context = []) { |
|
| 192 | - $this->log(ILogger::DEBUG, $message, $context); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Logs with an arbitrary level. |
|
| 198 | - * |
|
| 199 | - * @param int $level |
|
| 200 | - * @param string $message |
|
| 201 | - * @param array $context |
|
| 202 | - * @return void |
|
| 203 | - */ |
|
| 204 | - public function log(int $level, string $message, array $context = []) { |
|
| 205 | - $minLevel = $this->getLogLevel($context); |
|
| 206 | - |
|
| 207 | - array_walk($context, [$this->normalizer, 'format']); |
|
| 208 | - |
|
| 209 | - $app = $context['app'] ?? 'no app in context'; |
|
| 210 | - |
|
| 211 | - // interpolate $message as defined in PSR-3 |
|
| 212 | - $replace = []; |
|
| 213 | - foreach ($context as $key => $val) { |
|
| 214 | - $replace['{' . $key . '}'] = $val; |
|
| 215 | - } |
|
| 216 | - $message = strtr($message, $replace); |
|
| 217 | - |
|
| 218 | - try { |
|
| 219 | - if ($level >= $minLevel) { |
|
| 220 | - $this->writeLog($app, $message, $level); |
|
| 221 | - |
|
| 222 | - if ($this->crashReporters !== null) { |
|
| 223 | - $messageContext = array_merge( |
|
| 224 | - $context, |
|
| 225 | - [ |
|
| 226 | - 'level' => $level |
|
| 227 | - ] |
|
| 228 | - ); |
|
| 229 | - $this->crashReporters->delegateMessage($message, $messageContext); |
|
| 230 | - } |
|
| 231 | - } else { |
|
| 232 | - if ($this->crashReporters !== null) { |
|
| 233 | - $this->crashReporters->delegateBreadcrumb($message, 'log', $context); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - } catch (\Throwable $e) { |
|
| 237 | - // make sure we dont hard crash if logging fails |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - private function getLogLevel($context) { |
|
| 242 | - $logCondition = $this->config->getValue('log.condition', []); |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * check for a special log condition - this enables an increased log on |
|
| 246 | - * a per request/user base |
|
| 247 | - */ |
|
| 248 | - if ($this->logConditionSatisfied === null) { |
|
| 249 | - // default to false to just process this once per request |
|
| 250 | - $this->logConditionSatisfied = false; |
|
| 251 | - if (!empty($logCondition)) { |
|
| 252 | - |
|
| 253 | - // check for secret token in the request |
|
| 254 | - if (isset($logCondition['shared_secret'])) { |
|
| 255 | - $request = \OC::$server->getRequest(); |
|
| 256 | - |
|
| 257 | - if ($request->getMethod() === 'PUT' && |
|
| 258 | - strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false && |
|
| 259 | - strpos($request->getHeader('Content-Type'), 'application/json') === false) { |
|
| 260 | - $logSecretRequest = ''; |
|
| 261 | - } else { |
|
| 262 | - $logSecretRequest = $request->getParam('log_secret', ''); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - // if token is found in the request change set the log condition to satisfied |
|
| 266 | - if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) { |
|
| 267 | - $this->logConditionSatisfied = true; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - // check for user |
|
| 272 | - if (isset($logCondition['users'])) { |
|
| 273 | - $user = \OC::$server->getUserSession()->getUser(); |
|
| 274 | - |
|
| 275 | - // if the user matches set the log condition to satisfied |
|
| 276 | - if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
| 277 | - $this->logConditionSatisfied = true; |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - // if log condition is satisfied change the required log level to DEBUG |
|
| 284 | - if ($this->logConditionSatisfied) { |
|
| 285 | - return ILogger::DEBUG; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - if (isset($context['app'])) { |
|
| 289 | - $app = $context['app']; |
|
| 290 | - |
|
| 291 | - /** |
|
| 292 | - * check log condition based on the context of each log message |
|
| 293 | - * once this is met -> change the required log level to debug |
|
| 294 | - */ |
|
| 295 | - if (!empty($logCondition) |
|
| 296 | - && isset($logCondition['apps']) |
|
| 297 | - && in_array($app, $logCondition['apps'], true)) { |
|
| 298 | - return ILogger::DEBUG; |
|
| 299 | - } |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Logs an exception very detailed |
|
| 307 | - * |
|
| 308 | - * @param \Exception|\Throwable $exception |
|
| 309 | - * @param array $context |
|
| 310 | - * @return void |
|
| 311 | - * @since 8.2.0 |
|
| 312 | - */ |
|
| 313 | - public function logException(\Throwable $exception, array $context = []) { |
|
| 314 | - $app = $context['app'] ?? 'no app in context'; |
|
| 315 | - $level = $context['level'] ?? ILogger::ERROR; |
|
| 316 | - |
|
| 317 | - $serializer = new ExceptionSerializer($this->config); |
|
| 318 | - $data = $serializer->serializeException($exception); |
|
| 319 | - $data['CustomMessage'] = $context['message'] ?? '--'; |
|
| 320 | - |
|
| 321 | - $minLevel = $this->getLogLevel($context); |
|
| 322 | - |
|
| 323 | - array_walk($context, [$this->normalizer, 'format']); |
|
| 324 | - |
|
| 325 | - try { |
|
| 326 | - if ($level >= $minLevel) { |
|
| 327 | - if (!$this->logger instanceof IFileBased) { |
|
| 328 | - $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
| 329 | - } |
|
| 330 | - $this->writeLog($app, $data, $level); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - $context['level'] = $level; |
|
| 334 | - if (!is_null($this->crashReporters)) { |
|
| 335 | - $this->crashReporters->delegateReport($exception, $context); |
|
| 336 | - } |
|
| 337 | - } catch (\Throwable $e) { |
|
| 338 | - // make sure we dont hard crash if logging fails |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - public function logData(string $message, array $data, array $context = []): void { |
|
| 343 | - $app = $context['app'] ?? 'no app in context'; |
|
| 344 | - $level = $context['level'] ?? ILogger::ERROR; |
|
| 345 | - |
|
| 346 | - $minLevel = $this->getLogLevel($context); |
|
| 347 | - |
|
| 348 | - array_walk($context, [$this->normalizer, 'format']); |
|
| 349 | - |
|
| 350 | - try { |
|
| 351 | - if ($level >= $minLevel) { |
|
| 352 | - $data['message'] = $message; |
|
| 353 | - if (!$this->logger instanceof IFileBased) { |
|
| 354 | - $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
| 355 | - } |
|
| 356 | - $this->writeLog($app, $data, $level); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $context['level'] = $level; |
|
| 360 | - } catch (\Throwable $e) { |
|
| 361 | - // make sure we dont hard crash if logging fails |
|
| 362 | - } |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * @param string $app |
|
| 367 | - * @param string|array $entry |
|
| 368 | - * @param int $level |
|
| 369 | - */ |
|
| 370 | - protected function writeLog(string $app, $entry, int $level) { |
|
| 371 | - $this->logger->write($app, $entry, $level); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - public function getLogPath():string { |
|
| 375 | - if ($this->logger instanceof IFileBased) { |
|
| 376 | - return $this->logger->getLogFilePath(); |
|
| 377 | - } |
|
| 378 | - throw new \RuntimeException('Log implementation has no path'); |
|
| 379 | - } |
|
| 59 | + /** @var IWriter */ |
|
| 60 | + private $logger; |
|
| 61 | + |
|
| 62 | + /** @var SystemConfig */ |
|
| 63 | + private $config; |
|
| 64 | + |
|
| 65 | + /** @var boolean|null cache the result of the log condition check for the request */ |
|
| 66 | + private $logConditionSatisfied = null; |
|
| 67 | + |
|
| 68 | + /** @var Normalizer */ |
|
| 69 | + private $normalizer; |
|
| 70 | + |
|
| 71 | + /** @var IRegistry */ |
|
| 72 | + private $crashReporters; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param IWriter $logger The logger that should be used |
|
| 76 | + * @param SystemConfig $config the system config object |
|
| 77 | + * @param Normalizer|null $normalizer |
|
| 78 | + * @param IRegistry|null $registry |
|
| 79 | + */ |
|
| 80 | + public function __construct(IWriter $logger, SystemConfig $config = null, $normalizer = null, IRegistry $registry = null) { |
|
| 81 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
| 82 | + if ($config === null) { |
|
| 83 | + $config = \OC::$server->getSystemConfig(); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $this->config = $config; |
|
| 87 | + $this->logger = $logger; |
|
| 88 | + if ($normalizer === null) { |
|
| 89 | + $this->normalizer = new Normalizer(); |
|
| 90 | + } else { |
|
| 91 | + $this->normalizer = $normalizer; |
|
| 92 | + } |
|
| 93 | + $this->crashReporters = $registry; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * System is unusable. |
|
| 98 | + * |
|
| 99 | + * @param string $message |
|
| 100 | + * @param array $context |
|
| 101 | + * @return void |
|
| 102 | + */ |
|
| 103 | + public function emergency(string $message, array $context = []) { |
|
| 104 | + $this->log(ILogger::FATAL, $message, $context); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Action must be taken immediately. |
|
| 109 | + * |
|
| 110 | + * Example: Entire website down, database unavailable, etc. This should |
|
| 111 | + * trigger the SMS alerts and wake you up. |
|
| 112 | + * |
|
| 113 | + * @param string $message |
|
| 114 | + * @param array $context |
|
| 115 | + * @return void |
|
| 116 | + */ |
|
| 117 | + public function alert(string $message, array $context = []) { |
|
| 118 | + $this->log(ILogger::ERROR, $message, $context); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Critical conditions. |
|
| 123 | + * |
|
| 124 | + * Example: Application component unavailable, unexpected exception. |
|
| 125 | + * |
|
| 126 | + * @param string $message |
|
| 127 | + * @param array $context |
|
| 128 | + * @return void |
|
| 129 | + */ |
|
| 130 | + public function critical(string $message, array $context = []) { |
|
| 131 | + $this->log(ILogger::ERROR, $message, $context); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Runtime errors that do not require immediate action but should typically |
|
| 136 | + * be logged and monitored. |
|
| 137 | + * |
|
| 138 | + * @param string $message |
|
| 139 | + * @param array $context |
|
| 140 | + * @return void |
|
| 141 | + */ |
|
| 142 | + public function error(string $message, array $context = []) { |
|
| 143 | + $this->log(ILogger::ERROR, $message, $context); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Exceptional occurrences that are not errors. |
|
| 148 | + * |
|
| 149 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
| 150 | + * that are not necessarily wrong. |
|
| 151 | + * |
|
| 152 | + * @param string $message |
|
| 153 | + * @param array $context |
|
| 154 | + * @return void |
|
| 155 | + */ |
|
| 156 | + public function warning(string $message, array $context = []) { |
|
| 157 | + $this->log(ILogger::WARN, $message, $context); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Normal but significant events. |
|
| 162 | + * |
|
| 163 | + * @param string $message |
|
| 164 | + * @param array $context |
|
| 165 | + * @return void |
|
| 166 | + */ |
|
| 167 | + public function notice(string $message, array $context = []) { |
|
| 168 | + $this->log(ILogger::INFO, $message, $context); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Interesting events. |
|
| 173 | + * |
|
| 174 | + * Example: User logs in, SQL logs. |
|
| 175 | + * |
|
| 176 | + * @param string $message |
|
| 177 | + * @param array $context |
|
| 178 | + * @return void |
|
| 179 | + */ |
|
| 180 | + public function info(string $message, array $context = []) { |
|
| 181 | + $this->log(ILogger::INFO, $message, $context); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Detailed debug information. |
|
| 186 | + * |
|
| 187 | + * @param string $message |
|
| 188 | + * @param array $context |
|
| 189 | + * @return void |
|
| 190 | + */ |
|
| 191 | + public function debug(string $message, array $context = []) { |
|
| 192 | + $this->log(ILogger::DEBUG, $message, $context); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Logs with an arbitrary level. |
|
| 198 | + * |
|
| 199 | + * @param int $level |
|
| 200 | + * @param string $message |
|
| 201 | + * @param array $context |
|
| 202 | + * @return void |
|
| 203 | + */ |
|
| 204 | + public function log(int $level, string $message, array $context = []) { |
|
| 205 | + $minLevel = $this->getLogLevel($context); |
|
| 206 | + |
|
| 207 | + array_walk($context, [$this->normalizer, 'format']); |
|
| 208 | + |
|
| 209 | + $app = $context['app'] ?? 'no app in context'; |
|
| 210 | + |
|
| 211 | + // interpolate $message as defined in PSR-3 |
|
| 212 | + $replace = []; |
|
| 213 | + foreach ($context as $key => $val) { |
|
| 214 | + $replace['{' . $key . '}'] = $val; |
|
| 215 | + } |
|
| 216 | + $message = strtr($message, $replace); |
|
| 217 | + |
|
| 218 | + try { |
|
| 219 | + if ($level >= $minLevel) { |
|
| 220 | + $this->writeLog($app, $message, $level); |
|
| 221 | + |
|
| 222 | + if ($this->crashReporters !== null) { |
|
| 223 | + $messageContext = array_merge( |
|
| 224 | + $context, |
|
| 225 | + [ |
|
| 226 | + 'level' => $level |
|
| 227 | + ] |
|
| 228 | + ); |
|
| 229 | + $this->crashReporters->delegateMessage($message, $messageContext); |
|
| 230 | + } |
|
| 231 | + } else { |
|
| 232 | + if ($this->crashReporters !== null) { |
|
| 233 | + $this->crashReporters->delegateBreadcrumb($message, 'log', $context); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + } catch (\Throwable $e) { |
|
| 237 | + // make sure we dont hard crash if logging fails |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + private function getLogLevel($context) { |
|
| 242 | + $logCondition = $this->config->getValue('log.condition', []); |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * check for a special log condition - this enables an increased log on |
|
| 246 | + * a per request/user base |
|
| 247 | + */ |
|
| 248 | + if ($this->logConditionSatisfied === null) { |
|
| 249 | + // default to false to just process this once per request |
|
| 250 | + $this->logConditionSatisfied = false; |
|
| 251 | + if (!empty($logCondition)) { |
|
| 252 | + |
|
| 253 | + // check for secret token in the request |
|
| 254 | + if (isset($logCondition['shared_secret'])) { |
|
| 255 | + $request = \OC::$server->getRequest(); |
|
| 256 | + |
|
| 257 | + if ($request->getMethod() === 'PUT' && |
|
| 258 | + strpos($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') === false && |
|
| 259 | + strpos($request->getHeader('Content-Type'), 'application/json') === false) { |
|
| 260 | + $logSecretRequest = ''; |
|
| 261 | + } else { |
|
| 262 | + $logSecretRequest = $request->getParam('log_secret', ''); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // if token is found in the request change set the log condition to satisfied |
|
| 266 | + if ($request && hash_equals($logCondition['shared_secret'], $logSecretRequest)) { |
|
| 267 | + $this->logConditionSatisfied = true; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + // check for user |
|
| 272 | + if (isset($logCondition['users'])) { |
|
| 273 | + $user = \OC::$server->getUserSession()->getUser(); |
|
| 274 | + |
|
| 275 | + // if the user matches set the log condition to satisfied |
|
| 276 | + if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
| 277 | + $this->logConditionSatisfied = true; |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + // if log condition is satisfied change the required log level to DEBUG |
|
| 284 | + if ($this->logConditionSatisfied) { |
|
| 285 | + return ILogger::DEBUG; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + if (isset($context['app'])) { |
|
| 289 | + $app = $context['app']; |
|
| 290 | + |
|
| 291 | + /** |
|
| 292 | + * check log condition based on the context of each log message |
|
| 293 | + * once this is met -> change the required log level to debug |
|
| 294 | + */ |
|
| 295 | + if (!empty($logCondition) |
|
| 296 | + && isset($logCondition['apps']) |
|
| 297 | + && in_array($app, $logCondition['apps'], true)) { |
|
| 298 | + return ILogger::DEBUG; |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + return min($this->config->getValue('loglevel', ILogger::WARN), ILogger::FATAL); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Logs an exception very detailed |
|
| 307 | + * |
|
| 308 | + * @param \Exception|\Throwable $exception |
|
| 309 | + * @param array $context |
|
| 310 | + * @return void |
|
| 311 | + * @since 8.2.0 |
|
| 312 | + */ |
|
| 313 | + public function logException(\Throwable $exception, array $context = []) { |
|
| 314 | + $app = $context['app'] ?? 'no app in context'; |
|
| 315 | + $level = $context['level'] ?? ILogger::ERROR; |
|
| 316 | + |
|
| 317 | + $serializer = new ExceptionSerializer($this->config); |
|
| 318 | + $data = $serializer->serializeException($exception); |
|
| 319 | + $data['CustomMessage'] = $context['message'] ?? '--'; |
|
| 320 | + |
|
| 321 | + $minLevel = $this->getLogLevel($context); |
|
| 322 | + |
|
| 323 | + array_walk($context, [$this->normalizer, 'format']); |
|
| 324 | + |
|
| 325 | + try { |
|
| 326 | + if ($level >= $minLevel) { |
|
| 327 | + if (!$this->logger instanceof IFileBased) { |
|
| 328 | + $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
| 329 | + } |
|
| 330 | + $this->writeLog($app, $data, $level); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + $context['level'] = $level; |
|
| 334 | + if (!is_null($this->crashReporters)) { |
|
| 335 | + $this->crashReporters->delegateReport($exception, $context); |
|
| 336 | + } |
|
| 337 | + } catch (\Throwable $e) { |
|
| 338 | + // make sure we dont hard crash if logging fails |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + public function logData(string $message, array $data, array $context = []): void { |
|
| 343 | + $app = $context['app'] ?? 'no app in context'; |
|
| 344 | + $level = $context['level'] ?? ILogger::ERROR; |
|
| 345 | + |
|
| 346 | + $minLevel = $this->getLogLevel($context); |
|
| 347 | + |
|
| 348 | + array_walk($context, [$this->normalizer, 'format']); |
|
| 349 | + |
|
| 350 | + try { |
|
| 351 | + if ($level >= $minLevel) { |
|
| 352 | + $data['message'] = $message; |
|
| 353 | + if (!$this->logger instanceof IFileBased) { |
|
| 354 | + $data = json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); |
|
| 355 | + } |
|
| 356 | + $this->writeLog($app, $data, $level); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $context['level'] = $level; |
|
| 360 | + } catch (\Throwable $e) { |
|
| 361 | + // make sure we dont hard crash if logging fails |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * @param string $app |
|
| 367 | + * @param string|array $entry |
|
| 368 | + * @param int $level |
|
| 369 | + */ |
|
| 370 | + protected function writeLog(string $app, $entry, int $level) { |
|
| 371 | + $this->logger->write($app, $entry, $level); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + public function getLogPath():string { |
|
| 375 | + if ($this->logger instanceof IFileBased) { |
|
| 376 | + return $this->logger->getLogFilePath(); |
|
| 377 | + } |
|
| 378 | + throw new \RuntimeException('Log implementation has no path'); |
|
| 379 | + } |
|
| 380 | 380 | } |
@@ -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; |
@@ -37,49 +37,49 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | class SystemTagsEntityEvent extends Event { |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @deprecated 22.0.0 |
|
| 42 | - */ |
|
| 43 | - public const EVENT_ENTITY = 'OCP\SystemTag\ISystemTagManager::registerEntity'; |
|
| 40 | + /** |
|
| 41 | + * @deprecated 22.0.0 |
|
| 42 | + */ |
|
| 43 | + public const EVENT_ENTITY = 'OCP\SystemTag\ISystemTagManager::registerEntity'; |
|
| 44 | 44 | |
| 45 | - /** @var string */ |
|
| 46 | - protected $event; |
|
| 47 | - /** @var \Closure[] */ |
|
| 48 | - protected $collections; |
|
| 45 | + /** @var string */ |
|
| 46 | + protected $event; |
|
| 47 | + /** @var \Closure[] */ |
|
| 48 | + protected $collections; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * SystemTagsEntityEvent constructor. |
|
| 52 | - * |
|
| 53 | - * @param string $event |
|
| 54 | - * @since 9.1.0 |
|
| 55 | - */ |
|
| 56 | - public function __construct(string $event) { |
|
| 57 | - $this->event = $event; |
|
| 58 | - $this->collections = []; |
|
| 59 | - } |
|
| 50 | + /** |
|
| 51 | + * SystemTagsEntityEvent constructor. |
|
| 52 | + * |
|
| 53 | + * @param string $event |
|
| 54 | + * @since 9.1.0 |
|
| 55 | + */ |
|
| 56 | + public function __construct(string $event) { |
|
| 57 | + $this->event = $event; |
|
| 58 | + $this->collections = []; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param string $name |
|
| 63 | - * @param \Closure $entityExistsFunction The closure should take one |
|
| 64 | - * argument, which is the id of the entity, that tags |
|
| 65 | - * should be handled for. The return should then be bool, |
|
| 66 | - * depending on whether tags are allowed (true) or not. |
|
| 67 | - * @throws \OutOfBoundsException when the entity name is already taken |
|
| 68 | - * @since 9.1.0 |
|
| 69 | - */ |
|
| 70 | - public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
|
| 71 | - if (isset($this->collections[$name])) { |
|
| 72 | - throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
| 73 | - } |
|
| 61 | + /** |
|
| 62 | + * @param string $name |
|
| 63 | + * @param \Closure $entityExistsFunction The closure should take one |
|
| 64 | + * argument, which is the id of the entity, that tags |
|
| 65 | + * should be handled for. The return should then be bool, |
|
| 66 | + * depending on whether tags are allowed (true) or not. |
|
| 67 | + * @throws \OutOfBoundsException when the entity name is already taken |
|
| 68 | + * @since 9.1.0 |
|
| 69 | + */ |
|
| 70 | + public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
|
| 71 | + if (isset($this->collections[$name])) { |
|
| 72 | + throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $this->collections[$name] = $entityExistsFunction; |
|
| 76 | - } |
|
| 75 | + $this->collections[$name] = $entityExistsFunction; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @return \Closure[] |
|
| 80 | - * @since 9.1.0 |
|
| 81 | - */ |
|
| 82 | - public function getEntityCollections(): array { |
|
| 83 | - return $this->collections; |
|
| 84 | - } |
|
| 78 | + /** |
|
| 79 | + * @return \Closure[] |
|
| 80 | + * @since 9.1.0 |
|
| 81 | + */ |
|
| 82 | + public function getEntityCollections(): array { |
|
| 83 | + return $this->collections; |
|
| 84 | + } |
|
| 85 | 85 | } |
@@ -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 | } |
@@ -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 | } |
@@ -32,40 +32,40 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | trait RotationTrait { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var string |
|
| 37 | - * @since 14.0.0 |
|
| 38 | - */ |
|
| 39 | - protected $filePath; |
|
| 35 | + /** |
|
| 36 | + * @var string |
|
| 37 | + * @since 14.0.0 |
|
| 38 | + */ |
|
| 39 | + protected $filePath; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @var int |
|
| 43 | - * @since 14.0.0 |
|
| 44 | - */ |
|
| 45 | - protected $maxSize; |
|
| 41 | + /** |
|
| 42 | + * @var int |
|
| 43 | + * @since 14.0.0 |
|
| 44 | + */ |
|
| 45 | + protected $maxSize; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @return string the resulting new filepath |
|
| 49 | - * @since 14.0.0 |
|
| 50 | - */ |
|
| 51 | - protected function rotate():string { |
|
| 52 | - $rotatedFile = $this->filePath.'.1'; |
|
| 53 | - rename($this->filePath, $rotatedFile); |
|
| 54 | - return $rotatedFile; |
|
| 55 | - } |
|
| 47 | + /** |
|
| 48 | + * @return string the resulting new filepath |
|
| 49 | + * @since 14.0.0 |
|
| 50 | + */ |
|
| 51 | + protected function rotate():string { |
|
| 52 | + $rotatedFile = $this->filePath.'.1'; |
|
| 53 | + rename($this->filePath, $rotatedFile); |
|
| 54 | + return $rotatedFile; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return bool |
|
| 59 | - * @since 14.0.0 |
|
| 60 | - */ |
|
| 61 | - protected function shouldRotateBySize():bool { |
|
| 62 | - if ((int)$this->maxSize > 0) { |
|
| 63 | - $filesize = @filesize($this->filePath); |
|
| 64 | - if ($filesize >= (int)$this->maxSize) { |
|
| 65 | - return true; |
|
| 66 | - } |
|
| 67 | - } |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 57 | + /** |
|
| 58 | + * @return bool |
|
| 59 | + * @since 14.0.0 |
|
| 60 | + */ |
|
| 61 | + protected function shouldRotateBySize():bool { |
|
| 62 | + if ((int)$this->maxSize > 0) { |
|
| 63 | + $filesize = @filesize($this->filePath); |
|
| 64 | + if ($filesize >= (int)$this->maxSize) { |
|
| 65 | + return true; |
|
| 66 | + } |
|
| 67 | + } |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | } |
@@ -59,9 +59,9 @@ |
||
| 59 | 59 | * @since 14.0.0 |
| 60 | 60 | */ |
| 61 | 61 | protected function shouldRotateBySize():bool { |
| 62 | - if ((int)$this->maxSize > 0) { |
|
| 62 | + if ((int) $this->maxSize > 0) { |
|
| 63 | 63 | $filesize = @filesize($this->filePath); |
| 64 | - if ($filesize >= (int)$this->maxSize) { |
|
| 64 | + if ($filesize >= (int) $this->maxSize) { |
|
| 65 | 65 | return true; |
| 66 | 66 | } |
| 67 | 67 | } |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | |
| 22 | 22 | |
| 23 | -require_once __DIR__ . '/../lib/base.php'; |
|
| 23 | +require_once __DIR__.'/../lib/base.php'; |
|
| 24 | 24 | |
| 25 | 25 | header('Content-Type: application/json'); |
| 26 | 26 | |
@@ -29,12 +29,12 @@ |
||
| 29 | 29 | $isEnabled = $server->getAppManager()->isEnabledForUser('cloud_federation_api'); |
| 30 | 30 | |
| 31 | 31 | if ($isEnabled) { |
| 32 | - // Make sure the routes are loaded |
|
| 33 | - \OC_App::loadApp('cloud_federation_api'); |
|
| 34 | - $capabilities = new OCA\CloudFederationAPI\Capabilities($server->getURLGenerator()); |
|
| 35 | - header('Content-Type: application/json'); |
|
| 36 | - echo json_encode($capabilities->getCapabilities()['ocm']); |
|
| 32 | + // Make sure the routes are loaded |
|
| 33 | + \OC_App::loadApp('cloud_federation_api'); |
|
| 34 | + $capabilities = new OCA\CloudFederationAPI\Capabilities($server->getURLGenerator()); |
|
| 35 | + header('Content-Type: application/json'); |
|
| 36 | + echo json_encode($capabilities->getCapabilities()['ocm']); |
|
| 37 | 37 | } else { |
| 38 | - header($_SERVER["SERVER_PROTOCOL"]." 501 Not Implemented", true, 501); |
|
| 39 | - exit("501 Not Implemented"); |
|
| 38 | + header($_SERVER["SERVER_PROTOCOL"]." 501 Not Implemented", true, 501); |
|
| 39 | + exit("501 Not Implemented"); |
|
| 40 | 40 | } |
@@ -32,7 +32,7 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | public function __construct($providerId) { |
| 34 | 34 | $l = \OC::$server->getL10N('federation'); |
| 35 | - $message = 'Cloud Federation Provider with ID: "' . $providerId . '" does not exist.'; |
|
| 35 | + $message = 'Cloud Federation Provider with ID: "'.$providerId.'" does not exist.'; |
|
| 36 | 36 | $hint = $l->t('Cloud Federation Provider with ID: "%s" does not exist.', [$providerId]); |
| 37 | 37 | parent::__construct($message, $hint); |
| 38 | 38 | } |
@@ -32,18 +32,18 @@ |
||
| 32 | 32 | */ |
| 33 | 33 | class ProviderDoesNotExistsException extends HintException { |
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * ProviderDoesNotExistsException constructor. |
|
| 37 | - * |
|
| 38 | - * @since 14.0.0 |
|
| 39 | - * |
|
| 40 | - * @param string $providerId cloud federation provider ID |
|
| 41 | - */ |
|
| 42 | - public function __construct($providerId) { |
|
| 43 | - $l = \OC::$server->getL10N('federation'); |
|
| 44 | - $message = 'Cloud Federation Provider with ID: "' . $providerId . '" does not exist.'; |
|
| 45 | - $hint = $l->t('Cloud Federation Provider with ID: "%s" does not exist.', [$providerId]); |
|
| 46 | - parent::__construct($message, $hint); |
|
| 47 | - } |
|
| 35 | + /** |
|
| 36 | + * ProviderDoesNotExistsException constructor. |
|
| 37 | + * |
|
| 38 | + * @since 14.0.0 |
|
| 39 | + * |
|
| 40 | + * @param string $providerId cloud federation provider ID |
|
| 41 | + */ |
|
| 42 | + public function __construct($providerId) { |
|
| 43 | + $l = \OC::$server->getL10N('federation'); |
|
| 44 | + $message = 'Cloud Federation Provider with ID: "' . $providerId . '" does not exist.'; |
|
| 45 | + $hint = $l->t('Cloud Federation Provider with ID: "%s" does not exist.', [$providerId]); |
|
| 46 | + parent::__construct($message, $hint); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | } |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | $appName = $input->getArgument('app'); |
| 114 | 114 | $version = $input->getArgument('version'); |
| 115 | 115 | |
| 116 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 116 | + if (!preg_match('/^\d{1,16}$/', $version)) { |
|
| 117 | 117 | $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
| 118 | 118 | return 1; |
| 119 | 119 | } |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
| 122 | 122 | |
| 123 | 123 | $date = date('YmdHis'); |
| 124 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 124 | + $path = $this->generateMigration($ms, 'Version'.$version.'Date'.$date); |
|
| 125 | 125 | |
| 126 | 126 | $output->writeln("New migration class has been generated to <info>$path</info>"); |
| 127 | 127 | return 0; |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
| 152 | 152 | |
| 153 | 153 | $version = explode('.', $this->appManager->getAppVersion($appName)); |
| 154 | - return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 154 | + return [$version[0].sprintf('%1$03d', $version[1])]; |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | return []; |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | */ |
| 166 | 166 | protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
| 167 | 167 | if ($schemaBody === '') { |
| 168 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 168 | + $schemaBody = "\t\t".'return null;'; |
|
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | $dir = $ms->getMigrationsDirectory(); |
| 184 | 184 | |
| 185 | 185 | $this->ensureMigrationDirExists($dir); |
| 186 | - $path = $dir . '/' . $className . '.php'; |
|
| 186 | + $path = $dir.'/'.$className.'.php'; |
|
| 187 | 187 | |
| 188 | 188 | if (file_put_contents($path, $code) === false) { |
| 189 | 189 | throw new RuntimeException('Failed to generate new migration step.'); |
@@ -38,8 +38,8 @@ discard block |
||
| 38 | 38 | use Symfony\Component\Console\Output\OutputInterface; |
| 39 | 39 | |
| 40 | 40 | class GenerateCommand extends Command implements CompletionAwareInterface { |
| 41 | - protected static $_templateSimple = |
|
| 42 | - '<?php |
|
| 41 | + protected static $_templateSimple = |
|
| 42 | + '<?php |
|
| 43 | 43 | |
| 44 | 44 | declare(strict_types=1); |
| 45 | 45 | |
@@ -83,129 +83,129 @@ discard block |
||
| 83 | 83 | } |
| 84 | 84 | '; |
| 85 | 85 | |
| 86 | - /** @var Connection */ |
|
| 87 | - protected $connection; |
|
| 88 | - |
|
| 89 | - /** @var IAppManager */ |
|
| 90 | - protected $appManager; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param Connection $connection |
|
| 94 | - * @param IAppManager $appManager |
|
| 95 | - */ |
|
| 96 | - public function __construct(Connection $connection, IAppManager $appManager) { |
|
| 97 | - $this->connection = $connection; |
|
| 98 | - $this->appManager = $appManager; |
|
| 99 | - |
|
| 100 | - parent::__construct(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - protected function configure() { |
|
| 104 | - $this |
|
| 105 | - ->setName('migrations:generate') |
|
| 106 | - ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 107 | - ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 108 | - ; |
|
| 109 | - |
|
| 110 | - parent::configure(); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 114 | - $appName = $input->getArgument('app'); |
|
| 115 | - $version = $input->getArgument('version'); |
|
| 116 | - |
|
| 117 | - if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 118 | - $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 119 | - return 1; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 123 | - |
|
| 124 | - $date = date('YmdHis'); |
|
| 125 | - $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 126 | - |
|
| 127 | - $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 128 | - return 0; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * @param string $optionName |
|
| 133 | - * @param CompletionContext $context |
|
| 134 | - * @return string[] |
|
| 135 | - */ |
|
| 136 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 137 | - return []; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param string $argumentName |
|
| 142 | - * @param CompletionContext $context |
|
| 143 | - * @return string[] |
|
| 144 | - */ |
|
| 145 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 146 | - if ($argumentName === 'app') { |
|
| 147 | - $allApps = \OC_App::getAllApps(); |
|
| 148 | - return array_diff($allApps, \OC_App::getEnabledApps(true, true)); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - if ($argumentName === 'version') { |
|
| 152 | - $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 153 | - |
|
| 154 | - $version = explode('.', $this->appManager->getAppVersion($appName)); |
|
| 155 | - return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return []; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * @param MigrationService $ms |
|
| 163 | - * @param string $className |
|
| 164 | - * @param string $schemaBody |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 168 | - if ($schemaBody === '') { |
|
| 169 | - $schemaBody = "\t\t" . 'return null;'; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - $placeHolders = [ |
|
| 174 | - '{{namespace}}', |
|
| 175 | - '{{classname}}', |
|
| 176 | - '{{schemabody}}', |
|
| 177 | - ]; |
|
| 178 | - $replacements = [ |
|
| 179 | - $ms->getMigrationsNamespace(), |
|
| 180 | - $className, |
|
| 181 | - $schemaBody, |
|
| 182 | - ]; |
|
| 183 | - $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 184 | - $dir = $ms->getMigrationsDirectory(); |
|
| 185 | - |
|
| 186 | - $this->ensureMigrationDirExists($dir); |
|
| 187 | - $path = $dir . '/' . $className . '.php'; |
|
| 188 | - |
|
| 189 | - if (file_put_contents($path, $code) === false) { |
|
| 190 | - throw new RuntimeException('Failed to generate new migration step.'); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - return $path; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - protected function ensureMigrationDirExists($directory) { |
|
| 197 | - if (file_exists($directory) && is_dir($directory)) { |
|
| 198 | - return; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if (file_exists($directory)) { |
|
| 202 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $this->ensureMigrationDirExists(dirname($directory)); |
|
| 206 | - |
|
| 207 | - if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 208 | - throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 209 | - } |
|
| 210 | - } |
|
| 86 | + /** @var Connection */ |
|
| 87 | + protected $connection; |
|
| 88 | + |
|
| 89 | + /** @var IAppManager */ |
|
| 90 | + protected $appManager; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param Connection $connection |
|
| 94 | + * @param IAppManager $appManager |
|
| 95 | + */ |
|
| 96 | + public function __construct(Connection $connection, IAppManager $appManager) { |
|
| 97 | + $this->connection = $connection; |
|
| 98 | + $this->appManager = $appManager; |
|
| 99 | + |
|
| 100 | + parent::__construct(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + protected function configure() { |
|
| 104 | + $this |
|
| 105 | + ->setName('migrations:generate') |
|
| 106 | + ->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on') |
|
| 107 | + ->addArgument('version', InputArgument::REQUIRED, 'Major version of this app, to allow versions on parallel development branches') |
|
| 108 | + ; |
|
| 109 | + |
|
| 110 | + parent::configure(); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 114 | + $appName = $input->getArgument('app'); |
|
| 115 | + $version = $input->getArgument('version'); |
|
| 116 | + |
|
| 117 | + if (!preg_match('/^\d{1,16}$/',$version)) { |
|
| 118 | + $output->writeln('<error>The given version is invalid. Only 0-9 are allowed (max. 16 digits)</error>'); |
|
| 119 | + return 1; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $ms = new MigrationService($appName, $this->connection, new ConsoleOutput($output)); |
|
| 123 | + |
|
| 124 | + $date = date('YmdHis'); |
|
| 125 | + $path = $this->generateMigration($ms, 'Version' . $version . 'Date' . $date); |
|
| 126 | + |
|
| 127 | + $output->writeln("New migration class has been generated to <info>$path</info>"); |
|
| 128 | + return 0; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * @param string $optionName |
|
| 133 | + * @param CompletionContext $context |
|
| 134 | + * @return string[] |
|
| 135 | + */ |
|
| 136 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 137 | + return []; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param string $argumentName |
|
| 142 | + * @param CompletionContext $context |
|
| 143 | + * @return string[] |
|
| 144 | + */ |
|
| 145 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 146 | + if ($argumentName === 'app') { |
|
| 147 | + $allApps = \OC_App::getAllApps(); |
|
| 148 | + return array_diff($allApps, \OC_App::getEnabledApps(true, true)); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + if ($argumentName === 'version') { |
|
| 152 | + $appName = $context->getWordAtIndex($context->getWordIndex() - 1); |
|
| 153 | + |
|
| 154 | + $version = explode('.', $this->appManager->getAppVersion($appName)); |
|
| 155 | + return [$version[0] . sprintf('%1$03d', $version[1])]; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return []; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * @param MigrationService $ms |
|
| 163 | + * @param string $className |
|
| 164 | + * @param string $schemaBody |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + protected function generateMigration(MigrationService $ms, $className, $schemaBody = '') { |
|
| 168 | + if ($schemaBody === '') { |
|
| 169 | + $schemaBody = "\t\t" . 'return null;'; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + $placeHolders = [ |
|
| 174 | + '{{namespace}}', |
|
| 175 | + '{{classname}}', |
|
| 176 | + '{{schemabody}}', |
|
| 177 | + ]; |
|
| 178 | + $replacements = [ |
|
| 179 | + $ms->getMigrationsNamespace(), |
|
| 180 | + $className, |
|
| 181 | + $schemaBody, |
|
| 182 | + ]; |
|
| 183 | + $code = str_replace($placeHolders, $replacements, self::$_templateSimple); |
|
| 184 | + $dir = $ms->getMigrationsDirectory(); |
|
| 185 | + |
|
| 186 | + $this->ensureMigrationDirExists($dir); |
|
| 187 | + $path = $dir . '/' . $className . '.php'; |
|
| 188 | + |
|
| 189 | + if (file_put_contents($path, $code) === false) { |
|
| 190 | + throw new RuntimeException('Failed to generate new migration step.'); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + return $path; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + protected function ensureMigrationDirExists($directory) { |
|
| 197 | + if (file_exists($directory) && is_dir($directory)) { |
|
| 198 | + return; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if (file_exists($directory)) { |
|
| 202 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $this->ensureMigrationDirExists(dirname($directory)); |
|
| 206 | + |
|
| 207 | + if (!@mkdir($directory) && !is_dir($directory)) { |
|
| 208 | + throw new \RuntimeException("Could not create folder \"$directory\""); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | } |