1
0

App.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\AppFramework;
  29. use OC\AppFramework\Http\Dispatcher;
  30. use OC\AppFramework\DependencyInjection\DIContainer;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\QueryException;
  33. use OCP\AppFramework\Http\ICallbackResponse;
  34. use OCP\AppFramework\Http\IOutput;
  35. use OCP\IRequest;
  36. /**
  37. * Entry point for every request in your app. You can consider this as your
  38. * public static void main() method
  39. *
  40. * Handles all the dependency injection, controllers and output flow
  41. */
  42. class App {
  43. /** @var string[] */
  44. private static $nameSpaceCache = [];
  45. /**
  46. * Turns an app id into a namespace by either reading the appinfo.xml's
  47. * namespace tag or uppercasing the appid's first letter
  48. * @param string $appId the app id
  49. * @param string $topNamespace the namespace which should be prepended to
  50. * the transformed app id, defaults to OCA\
  51. * @return string the starting namespace for the app
  52. */
  53. public static function buildAppNamespace(string $appId, string $topNamespace='OCA\\'): string {
  54. // Hit the cache!
  55. if (isset(self::$nameSpaceCache[$appId])) {
  56. return $topNamespace . self::$nameSpaceCache[$appId];
  57. }
  58. $appInfo = \OC_App::getAppInfo($appId);
  59. if (isset($appInfo['namespace'])) {
  60. self::$nameSpaceCache[$appId] = trim($appInfo['namespace']);
  61. } else {
  62. // if the tag is not found, fall back to uppercasing the first letter
  63. self::$nameSpaceCache[$appId] = ucfirst($appId);
  64. }
  65. return $topNamespace . self::$nameSpaceCache[$appId];
  66. }
  67. /**
  68. * Shortcut for calling a controller method and printing the result
  69. * @param string $controllerName the name of the controller under which it is
  70. * stored in the DI container
  71. * @param string $methodName the method that you want to call
  72. * @param DIContainer $container an instance of a pimple container.
  73. * @param array $urlParams list of URL parameters (optional)
  74. */
  75. public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) {
  76. if (!is_null($urlParams)) {
  77. $container->query(IRequest::class)->setUrlParameters($urlParams);
  78. } else if (isset($container['urlParams']) && !is_null($container['urlParams'])) {
  79. $container->query(IRequest::class)->setUrlParameters($container['urlParams']);
  80. }
  81. $appName = $container['AppName'];
  82. // first try $controllerName then go for \OCA\AppName\Controller\$controllerName
  83. try {
  84. $controller = $container->query($controllerName);
  85. } catch(QueryException $e) {
  86. if ($appName === 'core') {
  87. $appNameSpace = 'OC\\Core';
  88. } else if ($appName === 'settings') {
  89. $appNameSpace = 'OC\\Settings';
  90. } else {
  91. $appNameSpace = self::buildAppNamespace($appName);
  92. }
  93. $controllerName = $appNameSpace . '\\Controller\\' . $controllerName;
  94. $controller = $container->query($controllerName);
  95. }
  96. // initialize the dispatcher and run all the middleware before the controller
  97. /** @var Dispatcher $dispatcher */
  98. $dispatcher = $container['Dispatcher'];
  99. list(
  100. $httpHeaders,
  101. $responseHeaders,
  102. $responseCookies,
  103. $output,
  104. $response
  105. ) = $dispatcher->dispatch($controller, $methodName);
  106. $io = $container[IOutput::class];
  107. if(!is_null($httpHeaders)) {
  108. $io->setHeader($httpHeaders);
  109. }
  110. foreach($responseHeaders as $name => $value) {
  111. $io->setHeader($name . ': ' . $value);
  112. }
  113. foreach($responseCookies as $name => $value) {
  114. $expireDate = null;
  115. if($value['expireDate'] instanceof \DateTime) {
  116. $expireDate = $value['expireDate']->getTimestamp();
  117. }
  118. $io->setCookie(
  119. $name,
  120. $value['value'],
  121. $expireDate,
  122. $container->getServer()->getWebRoot(),
  123. null,
  124. $container->getServer()->getRequest()->getServerProtocol() === 'https',
  125. true
  126. );
  127. }
  128. /*
  129. * Status 204 does not have a body and no Content Length
  130. * Status 304 does not have a body and does not need a Content Length
  131. * https://tools.ietf.org/html/rfc7230#section-3.3
  132. * https://tools.ietf.org/html/rfc7230#section-3.3.2
  133. */
  134. if ($httpHeaders !== Http::STATUS_NO_CONTENT && $httpHeaders !== Http::STATUS_NOT_MODIFIED) {
  135. if ($response instanceof ICallbackResponse) {
  136. $response->callback($io);
  137. } else if (!is_null($output)) {
  138. $io->setHeader('Content-Length: ' . strlen($output));
  139. $io->setOutput($output);
  140. }
  141. }
  142. }
  143. /**
  144. * Shortcut for calling a controller method and printing the result.
  145. * Similar to App:main except that no headers will be sent.
  146. * This should be used for example when registering sections via
  147. * \OC\AppFramework\Core\API::registerAdmin()
  148. *
  149. * @param string $controllerName the name of the controller under which it is
  150. * stored in the DI container
  151. * @param string $methodName the method that you want to call
  152. * @param array $urlParams an array with variables extracted from the routes
  153. * @param DIContainer $container an instance of a pimple container.
  154. */
  155. public static function part(string $controllerName, string $methodName, array $urlParams,
  156. DIContainer $container){
  157. $container['urlParams'] = $urlParams;
  158. $controller = $container[$controllerName];
  159. $dispatcher = $container['Dispatcher'];
  160. list(, , $output) = $dispatcher->dispatch($controller, $methodName);
  161. return $output;
  162. }
  163. }