@@ -79,387 +79,387 @@ |
||
79 | 79 | */ |
80 | 80 | class DIContainer extends SimpleContainer implements IAppContainer { |
81 | 81 | |
82 | - /** |
|
83 | - * @var array |
|
84 | - */ |
|
85 | - private $middleWares = []; |
|
86 | - |
|
87 | - /** @var ServerContainer */ |
|
88 | - private $server; |
|
89 | - |
|
90 | - /** |
|
91 | - * Put your class dependencies in here |
|
92 | - * @param string $appName the name of the app |
|
93 | - * @param array $urlParams |
|
94 | - * @param ServerContainer|null $server |
|
95 | - */ |
|
96 | - public function __construct($appName, $urlParams = [], ServerContainer $server = null) { |
|
97 | - parent::__construct(); |
|
98 | - $this['AppName'] = $appName; |
|
99 | - $this['urlParams'] = $urlParams; |
|
100 | - |
|
101 | - $this->registerAlias('Request', IRequest::class); |
|
102 | - |
|
103 | - /** @var \OC\ServerContainer $server */ |
|
104 | - if ($server === null) { |
|
105 | - $server = \OC::$server; |
|
106 | - } |
|
107 | - $this->server = $server; |
|
108 | - $this->server->registerAppContainer($appName, $this); |
|
109 | - |
|
110 | - // aliases |
|
111 | - $this->registerAlias('appName', 'AppName'); |
|
112 | - $this->registerAlias('webRoot', 'WebRoot'); |
|
113 | - $this->registerAlias('userId', 'UserId'); |
|
114 | - |
|
115 | - /** |
|
116 | - * Core services |
|
117 | - */ |
|
118 | - $this->registerService(IOutput::class, function () { |
|
119 | - return new Output($this->getServer()->getWebRoot()); |
|
120 | - }); |
|
121 | - |
|
122 | - $this->registerService(Folder::class, function () { |
|
123 | - return $this->getServer()->getUserFolder(); |
|
124 | - }); |
|
125 | - |
|
126 | - $this->registerService(IAppData::class, function (ContainerInterface $c) { |
|
127 | - return $this->getServer()->getAppDataDir($c->get('AppName')); |
|
128 | - }); |
|
129 | - |
|
130 | - $this->registerService(IL10N::class, function (ContainerInterface $c) { |
|
131 | - return $this->getServer()->getL10N($c->get('AppName')); |
|
132 | - }); |
|
133 | - |
|
134 | - // Log wrappers |
|
135 | - $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
|
136 | - return new ScopedPsrLogger( |
|
137 | - $c->get(PsrLoggerAdapter::class), |
|
138 | - $c->get('AppName') |
|
139 | - ); |
|
140 | - }); |
|
141 | - $this->registerService(ILogger::class, function (ContainerInterface $c) { |
|
142 | - return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName')); |
|
143 | - }); |
|
144 | - |
|
145 | - $this->registerService(IServerContainer::class, function () { |
|
146 | - return $this->getServer(); |
|
147 | - }); |
|
148 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
149 | - |
|
150 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) { |
|
151 | - return $c->get(Manager::class); |
|
152 | - }); |
|
153 | - |
|
154 | - $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
|
155 | - return $c; |
|
156 | - }); |
|
157 | - $this->registerAlias(IAppContainer::class, ContainerInterface::class); |
|
158 | - |
|
159 | - // commonly used attributes |
|
160 | - $this->registerService('UserId', function (ContainerInterface $c) { |
|
161 | - return $c->get(IUserSession::class)->getSession()->get('user_id'); |
|
162 | - }); |
|
163 | - |
|
164 | - $this->registerService('WebRoot', function (ContainerInterface $c) { |
|
165 | - return $c->get(IServerContainer::class)->getWebRoot(); |
|
166 | - }); |
|
167 | - |
|
168 | - $this->registerService('OC_Defaults', function (ContainerInterface $c) { |
|
169 | - return $c->get(IServerContainer::class)->getThemingDefaults(); |
|
170 | - }); |
|
171 | - |
|
172 | - $this->registerService('Protocol', function (ContainerInterface $c) { |
|
173 | - /** @var \OC\Server $server */ |
|
174 | - $server = $c->get(IServerContainer::class); |
|
175 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
176 | - return new Http($_SERVER, $protocol); |
|
177 | - }); |
|
178 | - |
|
179 | - $this->registerService('Dispatcher', function (ContainerInterface $c) { |
|
180 | - return new Dispatcher( |
|
181 | - $c->get('Protocol'), |
|
182 | - $c->get(MiddlewareDispatcher::class), |
|
183 | - $c->get(IControllerMethodReflector::class), |
|
184 | - $c->get(IRequest::class), |
|
185 | - $c->get(IConfig::class), |
|
186 | - $c->get(IDBConnection::class), |
|
187 | - $c->get(LoggerInterface::class) |
|
188 | - ); |
|
189 | - }); |
|
190 | - |
|
191 | - /** |
|
192 | - * App Framework default arguments |
|
193 | - */ |
|
194 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
195 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
196 | - $this->registerParameter('corsMaxAge', 1728000); |
|
197 | - |
|
198 | - /** |
|
199 | - * Middleware |
|
200 | - */ |
|
201 | - $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
|
202 | - $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
|
203 | - $server = $this->getServer(); |
|
204 | - |
|
205 | - $dispatcher = new MiddlewareDispatcher(); |
|
206 | - |
|
207 | - $dispatcher->registerMiddleware( |
|
208 | - $c->get(OC\AppFramework\Middleware\CompressionMiddleware::class) |
|
209 | - ); |
|
210 | - |
|
211 | - $dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); |
|
212 | - |
|
213 | - $dispatcher->registerMiddleware( |
|
214 | - $c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
215 | - ); |
|
216 | - |
|
217 | - $dispatcher->registerMiddleware( |
|
218 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
219 | - $c->get(IRequest::class), |
|
220 | - $c->get(IControllerMethodReflector::class) |
|
221 | - ) |
|
222 | - ); |
|
223 | - $dispatcher->registerMiddleware( |
|
224 | - new CORSMiddleware( |
|
225 | - $c->get(IRequest::class), |
|
226 | - $c->get(IControllerMethodReflector::class), |
|
227 | - $c->get(IUserSession::class), |
|
228 | - $c->get(OC\Security\Bruteforce\Throttler::class) |
|
229 | - ) |
|
230 | - ); |
|
231 | - $dispatcher->registerMiddleware( |
|
232 | - new OCSMiddleware( |
|
233 | - $c->get(IRequest::class) |
|
234 | - ) |
|
235 | - ); |
|
236 | - |
|
237 | - $securityMiddleware = new SecurityMiddleware( |
|
238 | - $c->get(IRequest::class), |
|
239 | - $c->get(IControllerMethodReflector::class), |
|
240 | - $c->get(INavigationManager::class), |
|
241 | - $c->get(IURLGenerator::class), |
|
242 | - $server->query(ILogger::class), |
|
243 | - $c->get('AppName'), |
|
244 | - $server->getUserSession()->isLoggedIn(), |
|
245 | - $server->getGroupManager()->isAdmin($this->getUserId()), |
|
246 | - $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
247 | - $server->getAppManager(), |
|
248 | - $server->getL10N('lib') |
|
249 | - ); |
|
250 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
251 | - $dispatcher->registerMiddleware( |
|
252 | - new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
253 | - $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
254 | - $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
255 | - $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
256 | - ) |
|
257 | - ); |
|
258 | - $dispatcher->registerMiddleware( |
|
259 | - $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
260 | - ); |
|
261 | - $dispatcher->registerMiddleware( |
|
262 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
263 | - $c->get(IControllerMethodReflector::class), |
|
264 | - $c->get(ISession::class), |
|
265 | - $c->get(IUserSession::class), |
|
266 | - $c->get(ITimeFactory::class) |
|
267 | - ) |
|
268 | - ); |
|
269 | - $dispatcher->registerMiddleware( |
|
270 | - new TwoFactorMiddleware( |
|
271 | - $c->get(OC\Authentication\TwoFactorAuth\Manager::class), |
|
272 | - $c->get(IUserSession::class), |
|
273 | - $c->get(ISession::class), |
|
274 | - $c->get(IURLGenerator::class), |
|
275 | - $c->get(IControllerMethodReflector::class), |
|
276 | - $c->get(IRequest::class) |
|
277 | - ) |
|
278 | - ); |
|
279 | - $dispatcher->registerMiddleware( |
|
280 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
281 | - $c->get(IControllerMethodReflector::class), |
|
282 | - $c->get(OC\Security\Bruteforce\Throttler::class), |
|
283 | - $c->get(IRequest::class) |
|
284 | - ) |
|
285 | - ); |
|
286 | - $dispatcher->registerMiddleware( |
|
287 | - new RateLimitingMiddleware( |
|
288 | - $c->get(IRequest::class), |
|
289 | - $c->get(IUserSession::class), |
|
290 | - $c->get(IControllerMethodReflector::class), |
|
291 | - $c->get(OC\Security\RateLimiting\Limiter::class) |
|
292 | - ) |
|
293 | - ); |
|
294 | - $dispatcher->registerMiddleware( |
|
295 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
296 | - $c->get(IRequest::class), |
|
297 | - $c->get(ISession::class), |
|
298 | - $c->get(\OCP\IConfig::class) |
|
299 | - ) |
|
300 | - ); |
|
301 | - $dispatcher->registerMiddleware( |
|
302 | - $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
303 | - ); |
|
304 | - |
|
305 | - foreach ($this->middleWares as $middleWare) { |
|
306 | - $dispatcher->registerMiddleware($c->get($middleWare)); |
|
307 | - } |
|
308 | - |
|
309 | - $dispatcher->registerMiddleware( |
|
310 | - new SessionMiddleware( |
|
311 | - $c->get(IControllerMethodReflector::class), |
|
312 | - $c->get(ISession::class) |
|
313 | - ) |
|
314 | - ); |
|
315 | - return $dispatcher; |
|
316 | - }); |
|
317 | - |
|
318 | - $this->registerService(IAppConfig::class, function (ContainerInterface $c) { |
|
319 | - return new OC\AppFramework\Services\AppConfig( |
|
320 | - $c->get(IConfig::class), |
|
321 | - $c->get('AppName') |
|
322 | - ); |
|
323 | - }); |
|
324 | - $this->registerService(IInitialState::class, function (ContainerInterface $c) { |
|
325 | - return new OC\AppFramework\Services\InitialState( |
|
326 | - $c->get(IInitialStateService::class), |
|
327 | - $c->get('AppName') |
|
328 | - ); |
|
329 | - }); |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * @return \OCP\IServerContainer |
|
334 | - */ |
|
335 | - public function getServer() { |
|
336 | - return $this->server; |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * @param string $middleWare |
|
341 | - * @return boolean|null |
|
342 | - */ |
|
343 | - public function registerMiddleWare($middleWare) { |
|
344 | - if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
345 | - return false; |
|
346 | - } |
|
347 | - $this->middleWares[] = $middleWare; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * used to return the appname of the set application |
|
352 | - * @return string the name of your application |
|
353 | - */ |
|
354 | - public function getAppName() { |
|
355 | - return $this->query('AppName'); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * @deprecated use IUserSession->isLoggedIn() |
|
360 | - * @return boolean |
|
361 | - */ |
|
362 | - public function isLoggedIn() { |
|
363 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
368 | - * @return boolean |
|
369 | - */ |
|
370 | - public function isAdminUser() { |
|
371 | - $uid = $this->getUserId(); |
|
372 | - return \OC_User::isAdminUser($uid); |
|
373 | - } |
|
374 | - |
|
375 | - private function getUserId() { |
|
376 | - return $this->getServer()->getSession()->get('user_id'); |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * @deprecated use the ILogger instead |
|
381 | - * @param string $message |
|
382 | - * @param string $level |
|
383 | - * @return mixed |
|
384 | - */ |
|
385 | - public function log($message, $level) { |
|
386 | - switch ($level) { |
|
387 | - case 'debug': |
|
388 | - $level = ILogger::DEBUG; |
|
389 | - break; |
|
390 | - case 'info': |
|
391 | - $level = ILogger::INFO; |
|
392 | - break; |
|
393 | - case 'warn': |
|
394 | - $level = ILogger::WARN; |
|
395 | - break; |
|
396 | - case 'fatal': |
|
397 | - $level = ILogger::FATAL; |
|
398 | - break; |
|
399 | - default: |
|
400 | - $level = ILogger::ERROR; |
|
401 | - break; |
|
402 | - } |
|
403 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * Register a capability |
|
408 | - * |
|
409 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
410 | - */ |
|
411 | - public function registerCapability($serviceName) { |
|
412 | - $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
413 | - return $this->query($serviceName); |
|
414 | - }); |
|
415 | - } |
|
416 | - |
|
417 | - public function has($id): bool { |
|
418 | - if (parent::has($id)) { |
|
419 | - return true; |
|
420 | - } |
|
421 | - |
|
422 | - if ($this->server->has($id, true)) { |
|
423 | - return true; |
|
424 | - } |
|
425 | - |
|
426 | - return false; |
|
427 | - } |
|
428 | - |
|
429 | - public function query(string $name, bool $autoload = true) { |
|
430 | - try { |
|
431 | - return $this->queryNoFallback($name); |
|
432 | - } catch (QueryException $firstException) { |
|
433 | - try { |
|
434 | - return $this->getServer()->query($name, $autoload); |
|
435 | - } catch (QueryException $secondException) { |
|
436 | - if ($firstException->getCode() === 1) { |
|
437 | - throw $secondException; |
|
438 | - } |
|
439 | - throw $firstException; |
|
440 | - } |
|
441 | - } |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * @param string $name |
|
446 | - * @return mixed |
|
447 | - * @throws QueryException if the query could not be resolved |
|
448 | - */ |
|
449 | - public function queryNoFallback($name) { |
|
450 | - $name = $this->sanitizeName($name); |
|
451 | - |
|
452 | - if ($this->offsetExists($name)) { |
|
453 | - return parent::query($name); |
|
454 | - } elseif ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
455 | - return parent::query($name); |
|
456 | - } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
457 | - return parent::query($name); |
|
458 | - } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
459 | - return parent::query($name); |
|
460 | - } |
|
461 | - |
|
462 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
463 | - ' Class can not be instantiated', 1); |
|
464 | - } |
|
82 | + /** |
|
83 | + * @var array |
|
84 | + */ |
|
85 | + private $middleWares = []; |
|
86 | + |
|
87 | + /** @var ServerContainer */ |
|
88 | + private $server; |
|
89 | + |
|
90 | + /** |
|
91 | + * Put your class dependencies in here |
|
92 | + * @param string $appName the name of the app |
|
93 | + * @param array $urlParams |
|
94 | + * @param ServerContainer|null $server |
|
95 | + */ |
|
96 | + public function __construct($appName, $urlParams = [], ServerContainer $server = null) { |
|
97 | + parent::__construct(); |
|
98 | + $this['AppName'] = $appName; |
|
99 | + $this['urlParams'] = $urlParams; |
|
100 | + |
|
101 | + $this->registerAlias('Request', IRequest::class); |
|
102 | + |
|
103 | + /** @var \OC\ServerContainer $server */ |
|
104 | + if ($server === null) { |
|
105 | + $server = \OC::$server; |
|
106 | + } |
|
107 | + $this->server = $server; |
|
108 | + $this->server->registerAppContainer($appName, $this); |
|
109 | + |
|
110 | + // aliases |
|
111 | + $this->registerAlias('appName', 'AppName'); |
|
112 | + $this->registerAlias('webRoot', 'WebRoot'); |
|
113 | + $this->registerAlias('userId', 'UserId'); |
|
114 | + |
|
115 | + /** |
|
116 | + * Core services |
|
117 | + */ |
|
118 | + $this->registerService(IOutput::class, function () { |
|
119 | + return new Output($this->getServer()->getWebRoot()); |
|
120 | + }); |
|
121 | + |
|
122 | + $this->registerService(Folder::class, function () { |
|
123 | + return $this->getServer()->getUserFolder(); |
|
124 | + }); |
|
125 | + |
|
126 | + $this->registerService(IAppData::class, function (ContainerInterface $c) { |
|
127 | + return $this->getServer()->getAppDataDir($c->get('AppName')); |
|
128 | + }); |
|
129 | + |
|
130 | + $this->registerService(IL10N::class, function (ContainerInterface $c) { |
|
131 | + return $this->getServer()->getL10N($c->get('AppName')); |
|
132 | + }); |
|
133 | + |
|
134 | + // Log wrappers |
|
135 | + $this->registerService(LoggerInterface::class, function (ContainerInterface $c) { |
|
136 | + return new ScopedPsrLogger( |
|
137 | + $c->get(PsrLoggerAdapter::class), |
|
138 | + $c->get('AppName') |
|
139 | + ); |
|
140 | + }); |
|
141 | + $this->registerService(ILogger::class, function (ContainerInterface $c) { |
|
142 | + return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->get('AppName')); |
|
143 | + }); |
|
144 | + |
|
145 | + $this->registerService(IServerContainer::class, function () { |
|
146 | + return $this->getServer(); |
|
147 | + }); |
|
148 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
149 | + |
|
150 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function (ContainerInterface $c) { |
|
151 | + return $c->get(Manager::class); |
|
152 | + }); |
|
153 | + |
|
154 | + $this->registerService(ContainerInterface::class, function (ContainerInterface $c) { |
|
155 | + return $c; |
|
156 | + }); |
|
157 | + $this->registerAlias(IAppContainer::class, ContainerInterface::class); |
|
158 | + |
|
159 | + // commonly used attributes |
|
160 | + $this->registerService('UserId', function (ContainerInterface $c) { |
|
161 | + return $c->get(IUserSession::class)->getSession()->get('user_id'); |
|
162 | + }); |
|
163 | + |
|
164 | + $this->registerService('WebRoot', function (ContainerInterface $c) { |
|
165 | + return $c->get(IServerContainer::class)->getWebRoot(); |
|
166 | + }); |
|
167 | + |
|
168 | + $this->registerService('OC_Defaults', function (ContainerInterface $c) { |
|
169 | + return $c->get(IServerContainer::class)->getThemingDefaults(); |
|
170 | + }); |
|
171 | + |
|
172 | + $this->registerService('Protocol', function (ContainerInterface $c) { |
|
173 | + /** @var \OC\Server $server */ |
|
174 | + $server = $c->get(IServerContainer::class); |
|
175 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
176 | + return new Http($_SERVER, $protocol); |
|
177 | + }); |
|
178 | + |
|
179 | + $this->registerService('Dispatcher', function (ContainerInterface $c) { |
|
180 | + return new Dispatcher( |
|
181 | + $c->get('Protocol'), |
|
182 | + $c->get(MiddlewareDispatcher::class), |
|
183 | + $c->get(IControllerMethodReflector::class), |
|
184 | + $c->get(IRequest::class), |
|
185 | + $c->get(IConfig::class), |
|
186 | + $c->get(IDBConnection::class), |
|
187 | + $c->get(LoggerInterface::class) |
|
188 | + ); |
|
189 | + }); |
|
190 | + |
|
191 | + /** |
|
192 | + * App Framework default arguments |
|
193 | + */ |
|
194 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
195 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
196 | + $this->registerParameter('corsMaxAge', 1728000); |
|
197 | + |
|
198 | + /** |
|
199 | + * Middleware |
|
200 | + */ |
|
201 | + $this->registerAlias('MiddlewareDispatcher', MiddlewareDispatcher::class); |
|
202 | + $this->registerService(MiddlewareDispatcher::class, function (ContainerInterface $c) { |
|
203 | + $server = $this->getServer(); |
|
204 | + |
|
205 | + $dispatcher = new MiddlewareDispatcher(); |
|
206 | + |
|
207 | + $dispatcher->registerMiddleware( |
|
208 | + $c->get(OC\AppFramework\Middleware\CompressionMiddleware::class) |
|
209 | + ); |
|
210 | + |
|
211 | + $dispatcher->registerMiddleware($c->get(OC\AppFramework\Middleware\NotModifiedMiddleware::class)); |
|
212 | + |
|
213 | + $dispatcher->registerMiddleware( |
|
214 | + $c->get(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
215 | + ); |
|
216 | + |
|
217 | + $dispatcher->registerMiddleware( |
|
218 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
219 | + $c->get(IRequest::class), |
|
220 | + $c->get(IControllerMethodReflector::class) |
|
221 | + ) |
|
222 | + ); |
|
223 | + $dispatcher->registerMiddleware( |
|
224 | + new CORSMiddleware( |
|
225 | + $c->get(IRequest::class), |
|
226 | + $c->get(IControllerMethodReflector::class), |
|
227 | + $c->get(IUserSession::class), |
|
228 | + $c->get(OC\Security\Bruteforce\Throttler::class) |
|
229 | + ) |
|
230 | + ); |
|
231 | + $dispatcher->registerMiddleware( |
|
232 | + new OCSMiddleware( |
|
233 | + $c->get(IRequest::class) |
|
234 | + ) |
|
235 | + ); |
|
236 | + |
|
237 | + $securityMiddleware = new SecurityMiddleware( |
|
238 | + $c->get(IRequest::class), |
|
239 | + $c->get(IControllerMethodReflector::class), |
|
240 | + $c->get(INavigationManager::class), |
|
241 | + $c->get(IURLGenerator::class), |
|
242 | + $server->query(ILogger::class), |
|
243 | + $c->get('AppName'), |
|
244 | + $server->getUserSession()->isLoggedIn(), |
|
245 | + $server->getGroupManager()->isAdmin($this->getUserId()), |
|
246 | + $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
247 | + $server->getAppManager(), |
|
248 | + $server->getL10N('lib') |
|
249 | + ); |
|
250 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
251 | + $dispatcher->registerMiddleware( |
|
252 | + new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
253 | + $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
254 | + $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
255 | + $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
256 | + ) |
|
257 | + ); |
|
258 | + $dispatcher->registerMiddleware( |
|
259 | + $server->query(OC\AppFramework\Middleware\Security\FeaturePolicyMiddleware::class) |
|
260 | + ); |
|
261 | + $dispatcher->registerMiddleware( |
|
262 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
263 | + $c->get(IControllerMethodReflector::class), |
|
264 | + $c->get(ISession::class), |
|
265 | + $c->get(IUserSession::class), |
|
266 | + $c->get(ITimeFactory::class) |
|
267 | + ) |
|
268 | + ); |
|
269 | + $dispatcher->registerMiddleware( |
|
270 | + new TwoFactorMiddleware( |
|
271 | + $c->get(OC\Authentication\TwoFactorAuth\Manager::class), |
|
272 | + $c->get(IUserSession::class), |
|
273 | + $c->get(ISession::class), |
|
274 | + $c->get(IURLGenerator::class), |
|
275 | + $c->get(IControllerMethodReflector::class), |
|
276 | + $c->get(IRequest::class) |
|
277 | + ) |
|
278 | + ); |
|
279 | + $dispatcher->registerMiddleware( |
|
280 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
281 | + $c->get(IControllerMethodReflector::class), |
|
282 | + $c->get(OC\Security\Bruteforce\Throttler::class), |
|
283 | + $c->get(IRequest::class) |
|
284 | + ) |
|
285 | + ); |
|
286 | + $dispatcher->registerMiddleware( |
|
287 | + new RateLimitingMiddleware( |
|
288 | + $c->get(IRequest::class), |
|
289 | + $c->get(IUserSession::class), |
|
290 | + $c->get(IControllerMethodReflector::class), |
|
291 | + $c->get(OC\Security\RateLimiting\Limiter::class) |
|
292 | + ) |
|
293 | + ); |
|
294 | + $dispatcher->registerMiddleware( |
|
295 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
296 | + $c->get(IRequest::class), |
|
297 | + $c->get(ISession::class), |
|
298 | + $c->get(\OCP\IConfig::class) |
|
299 | + ) |
|
300 | + ); |
|
301 | + $dispatcher->registerMiddleware( |
|
302 | + $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
303 | + ); |
|
304 | + |
|
305 | + foreach ($this->middleWares as $middleWare) { |
|
306 | + $dispatcher->registerMiddleware($c->get($middleWare)); |
|
307 | + } |
|
308 | + |
|
309 | + $dispatcher->registerMiddleware( |
|
310 | + new SessionMiddleware( |
|
311 | + $c->get(IControllerMethodReflector::class), |
|
312 | + $c->get(ISession::class) |
|
313 | + ) |
|
314 | + ); |
|
315 | + return $dispatcher; |
|
316 | + }); |
|
317 | + |
|
318 | + $this->registerService(IAppConfig::class, function (ContainerInterface $c) { |
|
319 | + return new OC\AppFramework\Services\AppConfig( |
|
320 | + $c->get(IConfig::class), |
|
321 | + $c->get('AppName') |
|
322 | + ); |
|
323 | + }); |
|
324 | + $this->registerService(IInitialState::class, function (ContainerInterface $c) { |
|
325 | + return new OC\AppFramework\Services\InitialState( |
|
326 | + $c->get(IInitialStateService::class), |
|
327 | + $c->get('AppName') |
|
328 | + ); |
|
329 | + }); |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * @return \OCP\IServerContainer |
|
334 | + */ |
|
335 | + public function getServer() { |
|
336 | + return $this->server; |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * @param string $middleWare |
|
341 | + * @return boolean|null |
|
342 | + */ |
|
343 | + public function registerMiddleWare($middleWare) { |
|
344 | + if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
345 | + return false; |
|
346 | + } |
|
347 | + $this->middleWares[] = $middleWare; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * used to return the appname of the set application |
|
352 | + * @return string the name of your application |
|
353 | + */ |
|
354 | + public function getAppName() { |
|
355 | + return $this->query('AppName'); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * @deprecated use IUserSession->isLoggedIn() |
|
360 | + * @return boolean |
|
361 | + */ |
|
362 | + public function isLoggedIn() { |
|
363 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
368 | + * @return boolean |
|
369 | + */ |
|
370 | + public function isAdminUser() { |
|
371 | + $uid = $this->getUserId(); |
|
372 | + return \OC_User::isAdminUser($uid); |
|
373 | + } |
|
374 | + |
|
375 | + private function getUserId() { |
|
376 | + return $this->getServer()->getSession()->get('user_id'); |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * @deprecated use the ILogger instead |
|
381 | + * @param string $message |
|
382 | + * @param string $level |
|
383 | + * @return mixed |
|
384 | + */ |
|
385 | + public function log($message, $level) { |
|
386 | + switch ($level) { |
|
387 | + case 'debug': |
|
388 | + $level = ILogger::DEBUG; |
|
389 | + break; |
|
390 | + case 'info': |
|
391 | + $level = ILogger::INFO; |
|
392 | + break; |
|
393 | + case 'warn': |
|
394 | + $level = ILogger::WARN; |
|
395 | + break; |
|
396 | + case 'fatal': |
|
397 | + $level = ILogger::FATAL; |
|
398 | + break; |
|
399 | + default: |
|
400 | + $level = ILogger::ERROR; |
|
401 | + break; |
|
402 | + } |
|
403 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * Register a capability |
|
408 | + * |
|
409 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
410 | + */ |
|
411 | + public function registerCapability($serviceName) { |
|
412 | + $this->query('OC\CapabilitiesManager')->registerCapability(function () use ($serviceName) { |
|
413 | + return $this->query($serviceName); |
|
414 | + }); |
|
415 | + } |
|
416 | + |
|
417 | + public function has($id): bool { |
|
418 | + if (parent::has($id)) { |
|
419 | + return true; |
|
420 | + } |
|
421 | + |
|
422 | + if ($this->server->has($id, true)) { |
|
423 | + return true; |
|
424 | + } |
|
425 | + |
|
426 | + return false; |
|
427 | + } |
|
428 | + |
|
429 | + public function query(string $name, bool $autoload = true) { |
|
430 | + try { |
|
431 | + return $this->queryNoFallback($name); |
|
432 | + } catch (QueryException $firstException) { |
|
433 | + try { |
|
434 | + return $this->getServer()->query($name, $autoload); |
|
435 | + } catch (QueryException $secondException) { |
|
436 | + if ($firstException->getCode() === 1) { |
|
437 | + throw $secondException; |
|
438 | + } |
|
439 | + throw $firstException; |
|
440 | + } |
|
441 | + } |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * @param string $name |
|
446 | + * @return mixed |
|
447 | + * @throws QueryException if the query could not be resolved |
|
448 | + */ |
|
449 | + public function queryNoFallback($name) { |
|
450 | + $name = $this->sanitizeName($name); |
|
451 | + |
|
452 | + if ($this->offsetExists($name)) { |
|
453 | + return parent::query($name); |
|
454 | + } elseif ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
455 | + return parent::query($name); |
|
456 | + } elseif ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
457 | + return parent::query($name); |
|
458 | + } elseif (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
459 | + return parent::query($name); |
|
460 | + } |
|
461 | + |
|
462 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
463 | + ' Class can not be instantiated', 1); |
|
464 | + } |
|
465 | 465 | } |
@@ -49,193 +49,193 @@ |
||
49 | 49 | */ |
50 | 50 | class Dispatcher { |
51 | 51 | |
52 | - /** @var MiddlewareDispatcher */ |
|
53 | - private $middlewareDispatcher; |
|
54 | - |
|
55 | - /** @var Http */ |
|
56 | - private $protocol; |
|
57 | - |
|
58 | - /** @var ControllerMethodReflector */ |
|
59 | - private $reflector; |
|
60 | - |
|
61 | - /** @var IRequest */ |
|
62 | - private $request; |
|
63 | - |
|
64 | - /** @var IConfig */ |
|
65 | - private $config; |
|
66 | - |
|
67 | - /** @var IDBConnection|Connection */ |
|
68 | - private $connection; |
|
69 | - |
|
70 | - /** @var LoggerInterface */ |
|
71 | - private $logger; |
|
72 | - |
|
73 | - /** |
|
74 | - * @param Http $protocol the http protocol with contains all status headers |
|
75 | - * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
76 | - * runs the middleware |
|
77 | - * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
78 | - * the arguments for the controller |
|
79 | - * @param IRequest $request the incoming request |
|
80 | - * @param IConfig $config |
|
81 | - * @param IDBConnection $connection |
|
82 | - * @param LoggerInterface $logger |
|
83 | - */ |
|
84 | - public function __construct(Http $protocol, |
|
85 | - MiddlewareDispatcher $middlewareDispatcher, |
|
86 | - ControllerMethodReflector $reflector, |
|
87 | - IRequest $request, |
|
88 | - IConfig $config, |
|
89 | - IDBConnection $connection, |
|
90 | - LoggerInterface $logger) { |
|
91 | - $this->protocol = $protocol; |
|
92 | - $this->middlewareDispatcher = $middlewareDispatcher; |
|
93 | - $this->reflector = $reflector; |
|
94 | - $this->request = $request; |
|
95 | - $this->config = $config; |
|
96 | - $this->connection = $connection; |
|
97 | - $this->logger = $logger; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Handles a request and calls the dispatcher on the controller |
|
103 | - * @param Controller $controller the controller which will be called |
|
104 | - * @param string $methodName the method name which will be called on |
|
105 | - * the controller |
|
106 | - * @return array $array[0] contains a string with the http main header, |
|
107 | - * $array[1] contains headers in the form: $key => value, $array[2] contains |
|
108 | - * the response output |
|
109 | - * @throws \Exception |
|
110 | - */ |
|
111 | - public function dispatch(Controller $controller, string $methodName): array { |
|
112 | - $out = [null, [], null]; |
|
113 | - |
|
114 | - try { |
|
115 | - // prefill reflector with everything thats needed for the |
|
116 | - // middlewares |
|
117 | - $this->reflector->reflect($controller, $methodName); |
|
118 | - |
|
119 | - $this->middlewareDispatcher->beforeController($controller, |
|
120 | - $methodName); |
|
121 | - |
|
122 | - $databaseStatsBefore = []; |
|
123 | - if ($this->config->getSystemValueBool('debug', false)) { |
|
124 | - $databaseStatsBefore = $this->connection->getStats(); |
|
125 | - } |
|
126 | - |
|
127 | - $response = $this->executeController($controller, $methodName); |
|
128 | - |
|
129 | - if (!empty($databaseStatsBefore)) { |
|
130 | - $databaseStatsAfter = $this->connection->getStats(); |
|
131 | - $numBuilt = $databaseStatsAfter['built'] - $databaseStatsBefore['built']; |
|
132 | - $numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed']; |
|
133 | - |
|
134 | - if ($numBuilt > 50) { |
|
135 | - $this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.' , [ |
|
136 | - 'class' => (string) get_class($controller), |
|
137 | - 'method' => $methodName, |
|
138 | - 'count' => $numBuilt, |
|
139 | - ]); |
|
140 | - } |
|
141 | - |
|
142 | - if ($numExecuted > 100) { |
|
143 | - $this->logger->warning('Controller {class}::{method} executed {count} queries.' , [ |
|
144 | - 'class' => (string) get_class($controller), |
|
145 | - 'method' => $methodName, |
|
146 | - 'count' => $numExecuted, |
|
147 | - ]); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - // if an exception appears, the middleware checks if it can handle the |
|
152 | - // exception and creates a response. If no response is created, it is |
|
153 | - // assumed that theres no middleware who can handle it and the error is |
|
154 | - // thrown again |
|
155 | - } catch (\Exception $exception) { |
|
156 | - $response = $this->middlewareDispatcher->afterException( |
|
157 | - $controller, $methodName, $exception); |
|
158 | - } catch (\Throwable $throwable) { |
|
159 | - $exception = new \Exception($throwable->getMessage(), $throwable->getCode(), $throwable); |
|
160 | - $response = $this->middlewareDispatcher->afterException( |
|
161 | - $controller, $methodName, $exception); |
|
162 | - } |
|
163 | - |
|
164 | - $response = $this->middlewareDispatcher->afterController( |
|
165 | - $controller, $methodName, $response); |
|
166 | - |
|
167 | - // depending on the cache object the headers need to be changed |
|
168 | - $out[0] = $this->protocol->getStatusHeader($response->getStatus()); |
|
169 | - $out[1] = array_merge($response->getHeaders()); |
|
170 | - $out[2] = $response->getCookies(); |
|
171 | - $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
172 | - $controller, $methodName, $response->render() |
|
173 | - ); |
|
174 | - $out[4] = $response; |
|
175 | - |
|
176 | - return $out; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * Uses the reflected parameters, types and request parameters to execute |
|
182 | - * the controller |
|
183 | - * @param Controller $controller the controller to be executed |
|
184 | - * @param string $methodName the method on the controller that should be executed |
|
185 | - * @return Response |
|
186 | - */ |
|
187 | - private function executeController(Controller $controller, string $methodName): Response { |
|
188 | - $arguments = []; |
|
189 | - |
|
190 | - // valid types that will be casted |
|
191 | - $types = ['int', 'integer', 'bool', 'boolean', 'float']; |
|
192 | - |
|
193 | - foreach ($this->reflector->getParameters() as $param => $default) { |
|
194 | - |
|
195 | - // try to get the parameter from the request object and cast |
|
196 | - // it to the type annotated in the @param annotation |
|
197 | - $value = $this->request->getParam($param, $default); |
|
198 | - $type = $this->reflector->getType($param); |
|
199 | - |
|
200 | - // if this is submitted using GET or a POST form, 'false' should be |
|
201 | - // converted to false |
|
202 | - if (($type === 'bool' || $type === 'boolean') && |
|
203 | - $value === 'false' && |
|
204 | - ( |
|
205 | - $this->request->method === 'GET' || |
|
206 | - strpos($this->request->getHeader('Content-Type'), |
|
207 | - 'application/x-www-form-urlencoded') !== false |
|
208 | - ) |
|
209 | - ) { |
|
210 | - $value = false; |
|
211 | - } elseif ($value !== null && \in_array($type, $types, true)) { |
|
212 | - settype($value, $type); |
|
213 | - } |
|
214 | - |
|
215 | - $arguments[] = $value; |
|
216 | - } |
|
217 | - |
|
218 | - $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
219 | - |
|
220 | - // format response |
|
221 | - if ($response instanceof DataResponse || !($response instanceof Response)) { |
|
222 | - |
|
223 | - // get format from the url format or request format parameter |
|
224 | - $format = $this->request->getParam('format'); |
|
225 | - |
|
226 | - // if none is given try the first Accept header |
|
227 | - if ($format === null) { |
|
228 | - $headers = $this->request->getHeader('Accept'); |
|
229 | - $format = $controller->getResponderByHTTPHeader($headers, null); |
|
230 | - } |
|
231 | - |
|
232 | - if ($format !== null) { |
|
233 | - $response = $controller->buildResponse($response, $format); |
|
234 | - } else { |
|
235 | - $response = $controller->buildResponse($response); |
|
236 | - } |
|
237 | - } |
|
238 | - |
|
239 | - return $response; |
|
240 | - } |
|
52 | + /** @var MiddlewareDispatcher */ |
|
53 | + private $middlewareDispatcher; |
|
54 | + |
|
55 | + /** @var Http */ |
|
56 | + private $protocol; |
|
57 | + |
|
58 | + /** @var ControllerMethodReflector */ |
|
59 | + private $reflector; |
|
60 | + |
|
61 | + /** @var IRequest */ |
|
62 | + private $request; |
|
63 | + |
|
64 | + /** @var IConfig */ |
|
65 | + private $config; |
|
66 | + |
|
67 | + /** @var IDBConnection|Connection */ |
|
68 | + private $connection; |
|
69 | + |
|
70 | + /** @var LoggerInterface */ |
|
71 | + private $logger; |
|
72 | + |
|
73 | + /** |
|
74 | + * @param Http $protocol the http protocol with contains all status headers |
|
75 | + * @param MiddlewareDispatcher $middlewareDispatcher the dispatcher which |
|
76 | + * runs the middleware |
|
77 | + * @param ControllerMethodReflector $reflector the reflector that is used to inject |
|
78 | + * the arguments for the controller |
|
79 | + * @param IRequest $request the incoming request |
|
80 | + * @param IConfig $config |
|
81 | + * @param IDBConnection $connection |
|
82 | + * @param LoggerInterface $logger |
|
83 | + */ |
|
84 | + public function __construct(Http $protocol, |
|
85 | + MiddlewareDispatcher $middlewareDispatcher, |
|
86 | + ControllerMethodReflector $reflector, |
|
87 | + IRequest $request, |
|
88 | + IConfig $config, |
|
89 | + IDBConnection $connection, |
|
90 | + LoggerInterface $logger) { |
|
91 | + $this->protocol = $protocol; |
|
92 | + $this->middlewareDispatcher = $middlewareDispatcher; |
|
93 | + $this->reflector = $reflector; |
|
94 | + $this->request = $request; |
|
95 | + $this->config = $config; |
|
96 | + $this->connection = $connection; |
|
97 | + $this->logger = $logger; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Handles a request and calls the dispatcher on the controller |
|
103 | + * @param Controller $controller the controller which will be called |
|
104 | + * @param string $methodName the method name which will be called on |
|
105 | + * the controller |
|
106 | + * @return array $array[0] contains a string with the http main header, |
|
107 | + * $array[1] contains headers in the form: $key => value, $array[2] contains |
|
108 | + * the response output |
|
109 | + * @throws \Exception |
|
110 | + */ |
|
111 | + public function dispatch(Controller $controller, string $methodName): array { |
|
112 | + $out = [null, [], null]; |
|
113 | + |
|
114 | + try { |
|
115 | + // prefill reflector with everything thats needed for the |
|
116 | + // middlewares |
|
117 | + $this->reflector->reflect($controller, $methodName); |
|
118 | + |
|
119 | + $this->middlewareDispatcher->beforeController($controller, |
|
120 | + $methodName); |
|
121 | + |
|
122 | + $databaseStatsBefore = []; |
|
123 | + if ($this->config->getSystemValueBool('debug', false)) { |
|
124 | + $databaseStatsBefore = $this->connection->getStats(); |
|
125 | + } |
|
126 | + |
|
127 | + $response = $this->executeController($controller, $methodName); |
|
128 | + |
|
129 | + if (!empty($databaseStatsBefore)) { |
|
130 | + $databaseStatsAfter = $this->connection->getStats(); |
|
131 | + $numBuilt = $databaseStatsAfter['built'] - $databaseStatsBefore['built']; |
|
132 | + $numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed']; |
|
133 | + |
|
134 | + if ($numBuilt > 50) { |
|
135 | + $this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.' , [ |
|
136 | + 'class' => (string) get_class($controller), |
|
137 | + 'method' => $methodName, |
|
138 | + 'count' => $numBuilt, |
|
139 | + ]); |
|
140 | + } |
|
141 | + |
|
142 | + if ($numExecuted > 100) { |
|
143 | + $this->logger->warning('Controller {class}::{method} executed {count} queries.' , [ |
|
144 | + 'class' => (string) get_class($controller), |
|
145 | + 'method' => $methodName, |
|
146 | + 'count' => $numExecuted, |
|
147 | + ]); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + // if an exception appears, the middleware checks if it can handle the |
|
152 | + // exception and creates a response. If no response is created, it is |
|
153 | + // assumed that theres no middleware who can handle it and the error is |
|
154 | + // thrown again |
|
155 | + } catch (\Exception $exception) { |
|
156 | + $response = $this->middlewareDispatcher->afterException( |
|
157 | + $controller, $methodName, $exception); |
|
158 | + } catch (\Throwable $throwable) { |
|
159 | + $exception = new \Exception($throwable->getMessage(), $throwable->getCode(), $throwable); |
|
160 | + $response = $this->middlewareDispatcher->afterException( |
|
161 | + $controller, $methodName, $exception); |
|
162 | + } |
|
163 | + |
|
164 | + $response = $this->middlewareDispatcher->afterController( |
|
165 | + $controller, $methodName, $response); |
|
166 | + |
|
167 | + // depending on the cache object the headers need to be changed |
|
168 | + $out[0] = $this->protocol->getStatusHeader($response->getStatus()); |
|
169 | + $out[1] = array_merge($response->getHeaders()); |
|
170 | + $out[2] = $response->getCookies(); |
|
171 | + $out[3] = $this->middlewareDispatcher->beforeOutput( |
|
172 | + $controller, $methodName, $response->render() |
|
173 | + ); |
|
174 | + $out[4] = $response; |
|
175 | + |
|
176 | + return $out; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * Uses the reflected parameters, types and request parameters to execute |
|
182 | + * the controller |
|
183 | + * @param Controller $controller the controller to be executed |
|
184 | + * @param string $methodName the method on the controller that should be executed |
|
185 | + * @return Response |
|
186 | + */ |
|
187 | + private function executeController(Controller $controller, string $methodName): Response { |
|
188 | + $arguments = []; |
|
189 | + |
|
190 | + // valid types that will be casted |
|
191 | + $types = ['int', 'integer', 'bool', 'boolean', 'float']; |
|
192 | + |
|
193 | + foreach ($this->reflector->getParameters() as $param => $default) { |
|
194 | + |
|
195 | + // try to get the parameter from the request object and cast |
|
196 | + // it to the type annotated in the @param annotation |
|
197 | + $value = $this->request->getParam($param, $default); |
|
198 | + $type = $this->reflector->getType($param); |
|
199 | + |
|
200 | + // if this is submitted using GET or a POST form, 'false' should be |
|
201 | + // converted to false |
|
202 | + if (($type === 'bool' || $type === 'boolean') && |
|
203 | + $value === 'false' && |
|
204 | + ( |
|
205 | + $this->request->method === 'GET' || |
|
206 | + strpos($this->request->getHeader('Content-Type'), |
|
207 | + 'application/x-www-form-urlencoded') !== false |
|
208 | + ) |
|
209 | + ) { |
|
210 | + $value = false; |
|
211 | + } elseif ($value !== null && \in_array($type, $types, true)) { |
|
212 | + settype($value, $type); |
|
213 | + } |
|
214 | + |
|
215 | + $arguments[] = $value; |
|
216 | + } |
|
217 | + |
|
218 | + $response = \call_user_func_array([$controller, $methodName], $arguments); |
|
219 | + |
|
220 | + // format response |
|
221 | + if ($response instanceof DataResponse || !($response instanceof Response)) { |
|
222 | + |
|
223 | + // get format from the url format or request format parameter |
|
224 | + $format = $this->request->getParam('format'); |
|
225 | + |
|
226 | + // if none is given try the first Accept header |
|
227 | + if ($format === null) { |
|
228 | + $headers = $this->request->getHeader('Accept'); |
|
229 | + $format = $controller->getResponderByHTTPHeader($headers, null); |
|
230 | + } |
|
231 | + |
|
232 | + if ($format !== null) { |
|
233 | + $response = $controller->buildResponse($response, $format); |
|
234 | + } else { |
|
235 | + $response = $controller->buildResponse($response); |
|
236 | + } |
|
237 | + } |
|
238 | + |
|
239 | + return $response; |
|
240 | + } |
|
241 | 241 | } |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | $numExecuted = $databaseStatsAfter['executed'] - $databaseStatsBefore['executed']; |
133 | 133 | |
134 | 134 | if ($numBuilt > 50) { |
135 | - $this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.' , [ |
|
135 | + $this->logger->debug('Controller {class}::{method} created {count} QueryBuilder objects, please check if they are created inside a loop by accident.', [ |
|
136 | 136 | 'class' => (string) get_class($controller), |
137 | 137 | 'method' => $methodName, |
138 | 138 | 'count' => $numBuilt, |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | } |
141 | 141 | |
142 | 142 | if ($numExecuted > 100) { |
143 | - $this->logger->warning('Controller {class}::{method} executed {count} queries.' , [ |
|
143 | + $this->logger->warning('Controller {class}::{method} executed {count} queries.', [ |
|
144 | 144 | 'class' => (string) get_class($controller), |
145 | 145 | 'method' => $methodName, |
146 | 146 | 'count' => $numExecuted, |
@@ -47,424 +47,424 @@ |
||
47 | 47 | use OCP\PreConditionNotMetException; |
48 | 48 | |
49 | 49 | class Connection extends ReconnectWrapper implements IDBConnection { |
50 | - /** |
|
51 | - * @var string $tablePrefix |
|
52 | - */ |
|
53 | - protected $tablePrefix; |
|
54 | - |
|
55 | - /** |
|
56 | - * @var \OC\DB\Adapter $adapter |
|
57 | - */ |
|
58 | - protected $adapter; |
|
59 | - |
|
60 | - protected $lockedTable = null; |
|
61 | - |
|
62 | - /** @var int */ |
|
63 | - protected $queriesBuilt = 0; |
|
64 | - |
|
65 | - /** @var int */ |
|
66 | - protected $queriesExecuted = 0; |
|
67 | - |
|
68 | - public function connect() { |
|
69 | - try { |
|
70 | - return parent::connect(); |
|
71 | - } catch (DBALException $e) { |
|
72 | - // throw a new exception to prevent leaking info from the stacktrace |
|
73 | - throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - public function getStats(): array { |
|
78 | - return [ |
|
79 | - 'built' => $this->queriesBuilt, |
|
80 | - 'executed' => $this->queriesExecuted, |
|
81 | - ]; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Returns a QueryBuilder for the connection. |
|
86 | - * |
|
87 | - * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
88 | - */ |
|
89 | - public function getQueryBuilder() { |
|
90 | - $this->queriesBuilt++; |
|
91 | - return new QueryBuilder( |
|
92 | - $this, |
|
93 | - \OC::$server->getSystemConfig(), |
|
94 | - \OC::$server->getLogger() |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Gets the QueryBuilder for the connection. |
|
100 | - * |
|
101 | - * @return \Doctrine\DBAL\Query\QueryBuilder |
|
102 | - * @deprecated please use $this->getQueryBuilder() instead |
|
103 | - */ |
|
104 | - public function createQueryBuilder() { |
|
105 | - $backtrace = $this->getCallerBacktrace(); |
|
106 | - \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
107 | - $this->queriesBuilt++; |
|
108 | - return parent::createQueryBuilder(); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Gets the ExpressionBuilder for the connection. |
|
113 | - * |
|
114 | - * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
115 | - * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
116 | - */ |
|
117 | - public function getExpressionBuilder() { |
|
118 | - $backtrace = $this->getCallerBacktrace(); |
|
119 | - \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
120 | - $this->queriesBuilt++; |
|
121 | - return parent::getExpressionBuilder(); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
126 | - * |
|
127 | - * @return string |
|
128 | - */ |
|
129 | - protected function getCallerBacktrace() { |
|
130 | - $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
131 | - |
|
132 | - // 0 is the method where we use `getCallerBacktrace` |
|
133 | - // 1 is the target method which uses the method we want to log |
|
134 | - if (isset($traces[1])) { |
|
135 | - return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
136 | - } |
|
137 | - |
|
138 | - return ''; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - public function getPrefix() { |
|
145 | - return $this->tablePrefix; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Initializes a new instance of the Connection class. |
|
150 | - * |
|
151 | - * @param array $params The connection parameters. |
|
152 | - * @param \Doctrine\DBAL\Driver $driver |
|
153 | - * @param \Doctrine\DBAL\Configuration $config |
|
154 | - * @param \Doctrine\Common\EventManager $eventManager |
|
155 | - * @throws \Exception |
|
156 | - */ |
|
157 | - public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
158 | - EventManager $eventManager = null) { |
|
159 | - if (!isset($params['adapter'])) { |
|
160 | - throw new \Exception('adapter not set'); |
|
161 | - } |
|
162 | - if (!isset($params['tablePrefix'])) { |
|
163 | - throw new \Exception('tablePrefix not set'); |
|
164 | - } |
|
165 | - parent::__construct($params, $driver, $config, $eventManager); |
|
166 | - $this->adapter = new $params['adapter']($this); |
|
167 | - $this->tablePrefix = $params['tablePrefix']; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Prepares an SQL statement. |
|
172 | - * |
|
173 | - * @param string $statement The SQL statement to prepare. |
|
174 | - * @param int $limit |
|
175 | - * @param int $offset |
|
176 | - * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
177 | - */ |
|
178 | - public function prepare($statement, $limit=null, $offset=null) { |
|
179 | - if ($limit === -1) { |
|
180 | - $limit = null; |
|
181 | - } |
|
182 | - if (!is_null($limit)) { |
|
183 | - $platform = $this->getDatabasePlatform(); |
|
184 | - $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
185 | - } |
|
186 | - $statement = $this->replaceTablePrefix($statement); |
|
187 | - $statement = $this->adapter->fixupStatement($statement); |
|
188 | - |
|
189 | - return parent::prepare($statement); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Executes an, optionally parametrized, SQL query. |
|
194 | - * |
|
195 | - * If the query is parametrized, a prepared statement is used. |
|
196 | - * If an SQLLogger is configured, the execution is logged. |
|
197 | - * |
|
198 | - * @param string $query The SQL query to execute. |
|
199 | - * @param array $params The parameters to bind to the query, if any. |
|
200 | - * @param array $types The types the previous parameters are in. |
|
201 | - * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
202 | - * |
|
203 | - * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
204 | - * |
|
205 | - * @throws \Doctrine\DBAL\DBALException |
|
206 | - */ |
|
207 | - public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) { |
|
208 | - $query = $this->replaceTablePrefix($query); |
|
209 | - $query = $this->adapter->fixupStatement($query); |
|
210 | - $this->queriesExecuted++; |
|
211 | - return parent::executeQuery($query, $params, $types, $qcp); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
216 | - * and returns the number of affected rows. |
|
217 | - * |
|
218 | - * This method supports PDO binding types as well as DBAL mapping types. |
|
219 | - * |
|
220 | - * @param string $query The SQL query. |
|
221 | - * @param array $params The query parameters. |
|
222 | - * @param array $types The parameter types. |
|
223 | - * |
|
224 | - * @return integer The number of affected rows. |
|
225 | - * |
|
226 | - * @throws \Doctrine\DBAL\DBALException |
|
227 | - */ |
|
228 | - public function executeUpdate($query, array $params = [], array $types = []) { |
|
229 | - $query = $this->replaceTablePrefix($query); |
|
230 | - $query = $this->adapter->fixupStatement($query); |
|
231 | - $this->queriesExecuted++; |
|
232 | - return parent::executeUpdate($query, $params, $types); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
237 | - * depending on the underlying driver. |
|
238 | - * |
|
239 | - * Note: This method may not return a meaningful or consistent result across different drivers, |
|
240 | - * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
241 | - * columns or sequences. |
|
242 | - * |
|
243 | - * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
244 | - * @return string A string representation of the last inserted ID. |
|
245 | - */ |
|
246 | - public function lastInsertId($seqName = null) { |
|
247 | - if ($seqName) { |
|
248 | - $seqName = $this->replaceTablePrefix($seqName); |
|
249 | - } |
|
250 | - return $this->adapter->lastInsertId($seqName); |
|
251 | - } |
|
252 | - |
|
253 | - // internal use |
|
254 | - public function realLastInsertId($seqName = null) { |
|
255 | - return parent::lastInsertId($seqName); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
260 | - * it is needed that there is also a unique constraint on the values. Then this method will |
|
261 | - * catch the exception and return 0. |
|
262 | - * |
|
263 | - * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
264 | - * @param array $input data that should be inserted into the table (column name => value) |
|
265 | - * @param array|null $compare List of values that should be checked for "if not exists" |
|
266 | - * If this is null or an empty array, all keys of $input will be compared |
|
267 | - * Please note: text fields (clob) must not be used in the compare array |
|
268 | - * @return int number of inserted rows |
|
269 | - * @throws \Doctrine\DBAL\DBALException |
|
270 | - * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
271 | - */ |
|
272 | - public function insertIfNotExist($table, $input, array $compare = null) { |
|
273 | - return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
274 | - } |
|
275 | - |
|
276 | - public function insertIgnoreConflict(string $table, array $values) : int { |
|
277 | - return $this->adapter->insertIgnoreConflict($table, $values); |
|
278 | - } |
|
279 | - |
|
280 | - private function getType($value) { |
|
281 | - if (is_bool($value)) { |
|
282 | - return IQueryBuilder::PARAM_BOOL; |
|
283 | - } elseif (is_int($value)) { |
|
284 | - return IQueryBuilder::PARAM_INT; |
|
285 | - } else { |
|
286 | - return IQueryBuilder::PARAM_STR; |
|
287 | - } |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Insert or update a row value |
|
292 | - * |
|
293 | - * @param string $table |
|
294 | - * @param array $keys (column name => value) |
|
295 | - * @param array $values (column name => value) |
|
296 | - * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
297 | - * @return int number of new rows |
|
298 | - * @throws \Doctrine\DBAL\DBALException |
|
299 | - * @throws PreConditionNotMetException |
|
300 | - */ |
|
301 | - public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
302 | - try { |
|
303 | - $insertQb = $this->getQueryBuilder(); |
|
304 | - $insertQb->insert($table) |
|
305 | - ->values( |
|
306 | - array_map(function ($value) use ($insertQb) { |
|
307 | - return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
308 | - }, array_merge($keys, $values)) |
|
309 | - ); |
|
310 | - return $insertQb->execute(); |
|
311 | - } catch (ConstraintViolationException $e) { |
|
312 | - // value already exists, try update |
|
313 | - $updateQb = $this->getQueryBuilder(); |
|
314 | - $updateQb->update($table); |
|
315 | - foreach ($values as $name => $value) { |
|
316 | - $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
317 | - } |
|
318 | - $where = $updateQb->expr()->andX(); |
|
319 | - $whereValues = array_merge($keys, $updatePreconditionValues); |
|
320 | - foreach ($whereValues as $name => $value) { |
|
321 | - $where->add($updateQb->expr()->eq( |
|
322 | - $name, |
|
323 | - $updateQb->createNamedParameter($value, $this->getType($value)), |
|
324 | - $this->getType($value) |
|
325 | - )); |
|
326 | - } |
|
327 | - $updateQb->where($where); |
|
328 | - $affected = $updateQb->execute(); |
|
329 | - |
|
330 | - if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
331 | - throw new PreConditionNotMetException(); |
|
332 | - } |
|
333 | - |
|
334 | - return 0; |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * Create an exclusive read+write lock on a table |
|
340 | - * |
|
341 | - * @param string $tableName |
|
342 | - * @throws \BadMethodCallException When trying to acquire a second lock |
|
343 | - * @since 9.1.0 |
|
344 | - */ |
|
345 | - public function lockTable($tableName) { |
|
346 | - if ($this->lockedTable !== null) { |
|
347 | - throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
348 | - } |
|
349 | - |
|
350 | - $tableName = $this->tablePrefix . $tableName; |
|
351 | - $this->lockedTable = $tableName; |
|
352 | - $this->adapter->lockTable($tableName); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Release a previous acquired lock again |
|
357 | - * |
|
358 | - * @since 9.1.0 |
|
359 | - */ |
|
360 | - public function unlockTable() { |
|
361 | - $this->adapter->unlockTable(); |
|
362 | - $this->lockedTable = null; |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * returns the error code and message as a string for logging |
|
367 | - * works with DoctrineException |
|
368 | - * @return string |
|
369 | - */ |
|
370 | - public function getError() { |
|
371 | - $msg = $this->errorCode() . ': '; |
|
372 | - $errorInfo = $this->errorInfo(); |
|
373 | - if (is_array($errorInfo)) { |
|
374 | - $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
375 | - $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
376 | - $msg .= 'Driver Message = '.$errorInfo[2]; |
|
377 | - } |
|
378 | - return $msg; |
|
379 | - } |
|
380 | - |
|
381 | - /** |
|
382 | - * Drop a table from the database if it exists |
|
383 | - * |
|
384 | - * @param string $table table name without the prefix |
|
385 | - */ |
|
386 | - public function dropTable($table) { |
|
387 | - $table = $this->tablePrefix . trim($table); |
|
388 | - $schema = $this->getSchemaManager(); |
|
389 | - if ($schema->tablesExist([$table])) { |
|
390 | - $schema->dropTable($table); |
|
391 | - } |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Check if a table exists |
|
396 | - * |
|
397 | - * @param string $table table name without the prefix |
|
398 | - * @return bool |
|
399 | - */ |
|
400 | - public function tableExists($table) { |
|
401 | - $table = $this->tablePrefix . trim($table); |
|
402 | - $schema = $this->getSchemaManager(); |
|
403 | - return $schema->tablesExist([$table]); |
|
404 | - } |
|
405 | - |
|
406 | - // internal use |
|
407 | - /** |
|
408 | - * @param string $statement |
|
409 | - * @return string |
|
410 | - */ |
|
411 | - protected function replaceTablePrefix($statement) { |
|
412 | - return str_replace('*PREFIX*', $this->tablePrefix, $statement); |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Check if a transaction is active |
|
417 | - * |
|
418 | - * @return bool |
|
419 | - * @since 8.2.0 |
|
420 | - */ |
|
421 | - public function inTransaction() { |
|
422 | - return $this->getTransactionNestingLevel() > 0; |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * Escape a parameter to be used in a LIKE query |
|
427 | - * |
|
428 | - * @param string $param |
|
429 | - * @return string |
|
430 | - */ |
|
431 | - public function escapeLikeParameter($param) { |
|
432 | - return addcslashes($param, '\\_%'); |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Check whether or not the current database support 4byte wide unicode |
|
437 | - * |
|
438 | - * @return bool |
|
439 | - * @since 11.0.0 |
|
440 | - */ |
|
441 | - public function supports4ByteText() { |
|
442 | - if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
443 | - return true; |
|
444 | - } |
|
445 | - return $this->getParams()['charset'] === 'utf8mb4'; |
|
446 | - } |
|
447 | - |
|
448 | - |
|
449 | - /** |
|
450 | - * Create the schema of the connected database |
|
451 | - * |
|
452 | - * @return Schema |
|
453 | - */ |
|
454 | - public function createSchema() { |
|
455 | - $schemaManager = new MDB2SchemaManager($this); |
|
456 | - $migrator = $schemaManager->getMigrator(); |
|
457 | - return $migrator->createSchema(); |
|
458 | - } |
|
459 | - |
|
460 | - /** |
|
461 | - * Migrate the database to the given schema |
|
462 | - * |
|
463 | - * @param Schema $toSchema |
|
464 | - */ |
|
465 | - public function migrateToSchema(Schema $toSchema) { |
|
466 | - $schemaManager = new MDB2SchemaManager($this); |
|
467 | - $migrator = $schemaManager->getMigrator(); |
|
468 | - $migrator->migrate($toSchema); |
|
469 | - } |
|
50 | + /** |
|
51 | + * @var string $tablePrefix |
|
52 | + */ |
|
53 | + protected $tablePrefix; |
|
54 | + |
|
55 | + /** |
|
56 | + * @var \OC\DB\Adapter $adapter |
|
57 | + */ |
|
58 | + protected $adapter; |
|
59 | + |
|
60 | + protected $lockedTable = null; |
|
61 | + |
|
62 | + /** @var int */ |
|
63 | + protected $queriesBuilt = 0; |
|
64 | + |
|
65 | + /** @var int */ |
|
66 | + protected $queriesExecuted = 0; |
|
67 | + |
|
68 | + public function connect() { |
|
69 | + try { |
|
70 | + return parent::connect(); |
|
71 | + } catch (DBALException $e) { |
|
72 | + // throw a new exception to prevent leaking info from the stacktrace |
|
73 | + throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode()); |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + public function getStats(): array { |
|
78 | + return [ |
|
79 | + 'built' => $this->queriesBuilt, |
|
80 | + 'executed' => $this->queriesExecuted, |
|
81 | + ]; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Returns a QueryBuilder for the connection. |
|
86 | + * |
|
87 | + * @return \OCP\DB\QueryBuilder\IQueryBuilder |
|
88 | + */ |
|
89 | + public function getQueryBuilder() { |
|
90 | + $this->queriesBuilt++; |
|
91 | + return new QueryBuilder( |
|
92 | + $this, |
|
93 | + \OC::$server->getSystemConfig(), |
|
94 | + \OC::$server->getLogger() |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Gets the QueryBuilder for the connection. |
|
100 | + * |
|
101 | + * @return \Doctrine\DBAL\Query\QueryBuilder |
|
102 | + * @deprecated please use $this->getQueryBuilder() instead |
|
103 | + */ |
|
104 | + public function createQueryBuilder() { |
|
105 | + $backtrace = $this->getCallerBacktrace(); |
|
106 | + \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
107 | + $this->queriesBuilt++; |
|
108 | + return parent::createQueryBuilder(); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Gets the ExpressionBuilder for the connection. |
|
113 | + * |
|
114 | + * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder |
|
115 | + * @deprecated please use $this->getQueryBuilder()->expr() instead |
|
116 | + */ |
|
117 | + public function getExpressionBuilder() { |
|
118 | + $backtrace = $this->getCallerBacktrace(); |
|
119 | + \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]); |
|
120 | + $this->queriesBuilt++; |
|
121 | + return parent::getExpressionBuilder(); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Get the file and line that called the method where `getCallerBacktrace()` was used |
|
126 | + * |
|
127 | + * @return string |
|
128 | + */ |
|
129 | + protected function getCallerBacktrace() { |
|
130 | + $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); |
|
131 | + |
|
132 | + // 0 is the method where we use `getCallerBacktrace` |
|
133 | + // 1 is the target method which uses the method we want to log |
|
134 | + if (isset($traces[1])) { |
|
135 | + return $traces[1]['file'] . ':' . $traces[1]['line']; |
|
136 | + } |
|
137 | + |
|
138 | + return ''; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + public function getPrefix() { |
|
145 | + return $this->tablePrefix; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Initializes a new instance of the Connection class. |
|
150 | + * |
|
151 | + * @param array $params The connection parameters. |
|
152 | + * @param \Doctrine\DBAL\Driver $driver |
|
153 | + * @param \Doctrine\DBAL\Configuration $config |
|
154 | + * @param \Doctrine\Common\EventManager $eventManager |
|
155 | + * @throws \Exception |
|
156 | + */ |
|
157 | + public function __construct(array $params, Driver $driver, Configuration $config = null, |
|
158 | + EventManager $eventManager = null) { |
|
159 | + if (!isset($params['adapter'])) { |
|
160 | + throw new \Exception('adapter not set'); |
|
161 | + } |
|
162 | + if (!isset($params['tablePrefix'])) { |
|
163 | + throw new \Exception('tablePrefix not set'); |
|
164 | + } |
|
165 | + parent::__construct($params, $driver, $config, $eventManager); |
|
166 | + $this->adapter = new $params['adapter']($this); |
|
167 | + $this->tablePrefix = $params['tablePrefix']; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Prepares an SQL statement. |
|
172 | + * |
|
173 | + * @param string $statement The SQL statement to prepare. |
|
174 | + * @param int $limit |
|
175 | + * @param int $offset |
|
176 | + * @return \Doctrine\DBAL\Driver\Statement The prepared statement. |
|
177 | + */ |
|
178 | + public function prepare($statement, $limit=null, $offset=null) { |
|
179 | + if ($limit === -1) { |
|
180 | + $limit = null; |
|
181 | + } |
|
182 | + if (!is_null($limit)) { |
|
183 | + $platform = $this->getDatabasePlatform(); |
|
184 | + $statement = $platform->modifyLimitQuery($statement, $limit, $offset); |
|
185 | + } |
|
186 | + $statement = $this->replaceTablePrefix($statement); |
|
187 | + $statement = $this->adapter->fixupStatement($statement); |
|
188 | + |
|
189 | + return parent::prepare($statement); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Executes an, optionally parametrized, SQL query. |
|
194 | + * |
|
195 | + * If the query is parametrized, a prepared statement is used. |
|
196 | + * If an SQLLogger is configured, the execution is logged. |
|
197 | + * |
|
198 | + * @param string $query The SQL query to execute. |
|
199 | + * @param array $params The parameters to bind to the query, if any. |
|
200 | + * @param array $types The types the previous parameters are in. |
|
201 | + * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. |
|
202 | + * |
|
203 | + * @return \Doctrine\DBAL\Driver\Statement The executed statement. |
|
204 | + * |
|
205 | + * @throws \Doctrine\DBAL\DBALException |
|
206 | + */ |
|
207 | + public function executeQuery($query, array $params = [], $types = [], QueryCacheProfile $qcp = null) { |
|
208 | + $query = $this->replaceTablePrefix($query); |
|
209 | + $query = $this->adapter->fixupStatement($query); |
|
210 | + $this->queriesExecuted++; |
|
211 | + return parent::executeQuery($query, $params, $types, $qcp); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters |
|
216 | + * and returns the number of affected rows. |
|
217 | + * |
|
218 | + * This method supports PDO binding types as well as DBAL mapping types. |
|
219 | + * |
|
220 | + * @param string $query The SQL query. |
|
221 | + * @param array $params The query parameters. |
|
222 | + * @param array $types The parameter types. |
|
223 | + * |
|
224 | + * @return integer The number of affected rows. |
|
225 | + * |
|
226 | + * @throws \Doctrine\DBAL\DBALException |
|
227 | + */ |
|
228 | + public function executeUpdate($query, array $params = [], array $types = []) { |
|
229 | + $query = $this->replaceTablePrefix($query); |
|
230 | + $query = $this->adapter->fixupStatement($query); |
|
231 | + $this->queriesExecuted++; |
|
232 | + return parent::executeUpdate($query, $params, $types); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Returns the ID of the last inserted row, or the last value from a sequence object, |
|
237 | + * depending on the underlying driver. |
|
238 | + * |
|
239 | + * Note: This method may not return a meaningful or consistent result across different drivers, |
|
240 | + * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY |
|
241 | + * columns or sequences. |
|
242 | + * |
|
243 | + * @param string $seqName Name of the sequence object from which the ID should be returned. |
|
244 | + * @return string A string representation of the last inserted ID. |
|
245 | + */ |
|
246 | + public function lastInsertId($seqName = null) { |
|
247 | + if ($seqName) { |
|
248 | + $seqName = $this->replaceTablePrefix($seqName); |
|
249 | + } |
|
250 | + return $this->adapter->lastInsertId($seqName); |
|
251 | + } |
|
252 | + |
|
253 | + // internal use |
|
254 | + public function realLastInsertId($seqName = null) { |
|
255 | + return parent::lastInsertId($seqName); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance |
|
260 | + * it is needed that there is also a unique constraint on the values. Then this method will |
|
261 | + * catch the exception and return 0. |
|
262 | + * |
|
263 | + * @param string $table The table name (will replace *PREFIX* with the actual prefix) |
|
264 | + * @param array $input data that should be inserted into the table (column name => value) |
|
265 | + * @param array|null $compare List of values that should be checked for "if not exists" |
|
266 | + * If this is null or an empty array, all keys of $input will be compared |
|
267 | + * Please note: text fields (clob) must not be used in the compare array |
|
268 | + * @return int number of inserted rows |
|
269 | + * @throws \Doctrine\DBAL\DBALException |
|
270 | + * @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371 |
|
271 | + */ |
|
272 | + public function insertIfNotExist($table, $input, array $compare = null) { |
|
273 | + return $this->adapter->insertIfNotExist($table, $input, $compare); |
|
274 | + } |
|
275 | + |
|
276 | + public function insertIgnoreConflict(string $table, array $values) : int { |
|
277 | + return $this->adapter->insertIgnoreConflict($table, $values); |
|
278 | + } |
|
279 | + |
|
280 | + private function getType($value) { |
|
281 | + if (is_bool($value)) { |
|
282 | + return IQueryBuilder::PARAM_BOOL; |
|
283 | + } elseif (is_int($value)) { |
|
284 | + return IQueryBuilder::PARAM_INT; |
|
285 | + } else { |
|
286 | + return IQueryBuilder::PARAM_STR; |
|
287 | + } |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Insert or update a row value |
|
292 | + * |
|
293 | + * @param string $table |
|
294 | + * @param array $keys (column name => value) |
|
295 | + * @param array $values (column name => value) |
|
296 | + * @param array $updatePreconditionValues ensure values match preconditions (column name => value) |
|
297 | + * @return int number of new rows |
|
298 | + * @throws \Doctrine\DBAL\DBALException |
|
299 | + * @throws PreConditionNotMetException |
|
300 | + */ |
|
301 | + public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) { |
|
302 | + try { |
|
303 | + $insertQb = $this->getQueryBuilder(); |
|
304 | + $insertQb->insert($table) |
|
305 | + ->values( |
|
306 | + array_map(function ($value) use ($insertQb) { |
|
307 | + return $insertQb->createNamedParameter($value, $this->getType($value)); |
|
308 | + }, array_merge($keys, $values)) |
|
309 | + ); |
|
310 | + return $insertQb->execute(); |
|
311 | + } catch (ConstraintViolationException $e) { |
|
312 | + // value already exists, try update |
|
313 | + $updateQb = $this->getQueryBuilder(); |
|
314 | + $updateQb->update($table); |
|
315 | + foreach ($values as $name => $value) { |
|
316 | + $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value))); |
|
317 | + } |
|
318 | + $where = $updateQb->expr()->andX(); |
|
319 | + $whereValues = array_merge($keys, $updatePreconditionValues); |
|
320 | + foreach ($whereValues as $name => $value) { |
|
321 | + $where->add($updateQb->expr()->eq( |
|
322 | + $name, |
|
323 | + $updateQb->createNamedParameter($value, $this->getType($value)), |
|
324 | + $this->getType($value) |
|
325 | + )); |
|
326 | + } |
|
327 | + $updateQb->where($where); |
|
328 | + $affected = $updateQb->execute(); |
|
329 | + |
|
330 | + if ($affected === 0 && !empty($updatePreconditionValues)) { |
|
331 | + throw new PreConditionNotMetException(); |
|
332 | + } |
|
333 | + |
|
334 | + return 0; |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * Create an exclusive read+write lock on a table |
|
340 | + * |
|
341 | + * @param string $tableName |
|
342 | + * @throws \BadMethodCallException When trying to acquire a second lock |
|
343 | + * @since 9.1.0 |
|
344 | + */ |
|
345 | + public function lockTable($tableName) { |
|
346 | + if ($this->lockedTable !== null) { |
|
347 | + throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.'); |
|
348 | + } |
|
349 | + |
|
350 | + $tableName = $this->tablePrefix . $tableName; |
|
351 | + $this->lockedTable = $tableName; |
|
352 | + $this->adapter->lockTable($tableName); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Release a previous acquired lock again |
|
357 | + * |
|
358 | + * @since 9.1.0 |
|
359 | + */ |
|
360 | + public function unlockTable() { |
|
361 | + $this->adapter->unlockTable(); |
|
362 | + $this->lockedTable = null; |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * returns the error code and message as a string for logging |
|
367 | + * works with DoctrineException |
|
368 | + * @return string |
|
369 | + */ |
|
370 | + public function getError() { |
|
371 | + $msg = $this->errorCode() . ': '; |
|
372 | + $errorInfo = $this->errorInfo(); |
|
373 | + if (is_array($errorInfo)) { |
|
374 | + $msg .= 'SQLSTATE = '.$errorInfo[0] . ', '; |
|
375 | + $msg .= 'Driver Code = '.$errorInfo[1] . ', '; |
|
376 | + $msg .= 'Driver Message = '.$errorInfo[2]; |
|
377 | + } |
|
378 | + return $msg; |
|
379 | + } |
|
380 | + |
|
381 | + /** |
|
382 | + * Drop a table from the database if it exists |
|
383 | + * |
|
384 | + * @param string $table table name without the prefix |
|
385 | + */ |
|
386 | + public function dropTable($table) { |
|
387 | + $table = $this->tablePrefix . trim($table); |
|
388 | + $schema = $this->getSchemaManager(); |
|
389 | + if ($schema->tablesExist([$table])) { |
|
390 | + $schema->dropTable($table); |
|
391 | + } |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Check if a table exists |
|
396 | + * |
|
397 | + * @param string $table table name without the prefix |
|
398 | + * @return bool |
|
399 | + */ |
|
400 | + public function tableExists($table) { |
|
401 | + $table = $this->tablePrefix . trim($table); |
|
402 | + $schema = $this->getSchemaManager(); |
|
403 | + return $schema->tablesExist([$table]); |
|
404 | + } |
|
405 | + |
|
406 | + // internal use |
|
407 | + /** |
|
408 | + * @param string $statement |
|
409 | + * @return string |
|
410 | + */ |
|
411 | + protected function replaceTablePrefix($statement) { |
|
412 | + return str_replace('*PREFIX*', $this->tablePrefix, $statement); |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Check if a transaction is active |
|
417 | + * |
|
418 | + * @return bool |
|
419 | + * @since 8.2.0 |
|
420 | + */ |
|
421 | + public function inTransaction() { |
|
422 | + return $this->getTransactionNestingLevel() > 0; |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * Escape a parameter to be used in a LIKE query |
|
427 | + * |
|
428 | + * @param string $param |
|
429 | + * @return string |
|
430 | + */ |
|
431 | + public function escapeLikeParameter($param) { |
|
432 | + return addcslashes($param, '\\_%'); |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Check whether or not the current database support 4byte wide unicode |
|
437 | + * |
|
438 | + * @return bool |
|
439 | + * @since 11.0.0 |
|
440 | + */ |
|
441 | + public function supports4ByteText() { |
|
442 | + if (!$this->getDatabasePlatform() instanceof MySqlPlatform) { |
|
443 | + return true; |
|
444 | + } |
|
445 | + return $this->getParams()['charset'] === 'utf8mb4'; |
|
446 | + } |
|
447 | + |
|
448 | + |
|
449 | + /** |
|
450 | + * Create the schema of the connected database |
|
451 | + * |
|
452 | + * @return Schema |
|
453 | + */ |
|
454 | + public function createSchema() { |
|
455 | + $schemaManager = new MDB2SchemaManager($this); |
|
456 | + $migrator = $schemaManager->getMigrator(); |
|
457 | + return $migrator->createSchema(); |
|
458 | + } |
|
459 | + |
|
460 | + /** |
|
461 | + * Migrate the database to the given schema |
|
462 | + * |
|
463 | + * @param Schema $toSchema |
|
464 | + */ |
|
465 | + public function migrateToSchema(Schema $toSchema) { |
|
466 | + $schemaManager = new MDB2SchemaManager($this); |
|
467 | + $migrator = $schemaManager->getMigrator(); |
|
468 | + $migrator->migrate($toSchema); |
|
469 | + } |
|
470 | 470 | } |