You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\AppFramework;
  31. use OC\AppFramework\DependencyInjection\DIContainer;
  32. use OC\AppFramework\Http\Dispatcher;
  33. use OC\AppFramework\Http\Request;
  34. use OC\HintException;
  35. use OCP\AppFramework\Http;
  36. use OCP\AppFramework\Http\ICallbackResponse;
  37. use OCP\AppFramework\Http\IOutput;
  38. use OCP\AppFramework\QueryException;
  39. use OCP\IRequest;
  40. /**
  41. * Entry point for every request in your app. You can consider this as your
  42. * public static void main() method
  43. *
  44. * Handles all the dependency injection, controllers and output flow
  45. */
  46. class App {
  47. /** @var string[] */
  48. private static $nameSpaceCache = [];
  49. /**
  50. * Turns an app id into a namespace by either reading the appinfo.xml's
  51. * namespace tag or uppercasing the appid's first letter
  52. * @param string $appId the app id
  53. * @param string $topNamespace the namespace which should be prepended to
  54. * the transformed app id, defaults to OCA\
  55. * @return string the starting namespace for the app
  56. */
  57. public static function buildAppNamespace(string $appId, string $topNamespace = 'OCA\\'): string {
  58. // Hit the cache!
  59. if (isset(self::$nameSpaceCache[$appId])) {
  60. return $topNamespace . self::$nameSpaceCache[$appId];
  61. }
  62. $appInfo = \OC_App::getAppInfo($appId);
  63. if (isset($appInfo['namespace'])) {
  64. self::$nameSpaceCache[$appId] = trim($appInfo['namespace']);
  65. } else {
  66. if ($appId !== 'spreed') {
  67. // if the tag is not found, fall back to uppercasing the first letter
  68. self::$nameSpaceCache[$appId] = ucfirst($appId);
  69. } else {
  70. // For the Talk app (appid spreed) the above fallback doesn't work.
  71. // This leads to a problem when trying to install it freshly,
  72. // because the apps namespace is already registered before the
  73. // app is downloaded from the appstore, because of the hackish
  74. // global route index.php/call/{token} which is registered via
  75. // the core/routes.php so it does not have the app namespace.
  76. // @ref https://github.com/nextcloud/server/pull/19433
  77. self::$nameSpaceCache[$appId] = 'Talk';
  78. }
  79. }
  80. return $topNamespace . self::$nameSpaceCache[$appId];
  81. }
  82. public static function getAppIdForClass(string $className, string $topNamespace = 'OCA\\'): ?string {
  83. if (strpos($className, $topNamespace) !== 0) {
  84. return null;
  85. }
  86. foreach (self::$nameSpaceCache as $appId => $namespace) {
  87. if (strpos($className, $topNamespace . $namespace . '\\') === 0) {
  88. return $appId;
  89. }
  90. }
  91. return null;
  92. }
  93. /**
  94. * Shortcut for calling a controller method and printing the result
  95. * @param string $controllerName the name of the controller under which it is
  96. * stored in the DI container
  97. * @param string $methodName the method that you want to call
  98. * @param DIContainer $container an instance of a pimple container.
  99. * @param array $urlParams list of URL parameters (optional)
  100. * @throws HintException
  101. */
  102. public static function main(string $controllerName, string $methodName, DIContainer $container, array $urlParams = null) {
  103. if (!is_null($urlParams)) {
  104. /** @var Request $request */
  105. $request = $container->query(IRequest::class);
  106. $request->setUrlParameters($urlParams);
  107. } elseif (isset($container['urlParams']) && !is_null($container['urlParams'])) {
  108. /** @var Request $request */
  109. $request = $container->query(IRequest::class);
  110. $request->setUrlParameters($container['urlParams']);
  111. }
  112. $appName = $container['AppName'];
  113. // first try $controllerName then go for \OCA\AppName\Controller\$controllerName
  114. try {
  115. $controller = $container->query($controllerName);
  116. } catch (QueryException $e) {
  117. if (strpos($controllerName, '\\Controller\\') !== false) {
  118. // This is from a global registered app route that is not enabled.
  119. [/*OC(A)*/, $app, /* Controller/Name*/] = explode('\\', $controllerName, 3);
  120. throw new HintException('App ' . strtolower($app) . ' is not enabled');
  121. }
  122. if ($appName === 'core') {
  123. $appNameSpace = 'OC\\Core';
  124. } else {
  125. $appNameSpace = self::buildAppNamespace($appName);
  126. }
  127. $controllerName = $appNameSpace . '\\Controller\\' . $controllerName;
  128. $controller = $container->query($controllerName);
  129. }
  130. // initialize the dispatcher and run all the middleware before the controller
  131. /** @var Dispatcher $dispatcher */
  132. $dispatcher = $container['Dispatcher'];
  133. [
  134. $httpHeaders,
  135. $responseHeaders,
  136. $responseCookies,
  137. $output,
  138. $response
  139. ] = $dispatcher->dispatch($controller, $methodName);
  140. $io = $container[IOutput::class];
  141. if (!is_null($httpHeaders)) {
  142. $io->setHeader($httpHeaders);
  143. }
  144. foreach ($responseHeaders as $name => $value) {
  145. $io->setHeader($name . ': ' . $value);
  146. }
  147. foreach ($responseCookies as $name => $value) {
  148. $expireDate = null;
  149. if ($value['expireDate'] instanceof \DateTime) {
  150. $expireDate = $value['expireDate']->getTimestamp();
  151. }
  152. $sameSite = $value['sameSite'] ?? 'Lax';
  153. $io->setCookie(
  154. $name,
  155. $value['value'],
  156. $expireDate,
  157. $container->getServer()->getWebRoot(),
  158. null,
  159. $container->getServer()->getRequest()->getServerProtocol() === 'https',
  160. true,
  161. $sameSite
  162. );
  163. }
  164. /*
  165. * Status 204 does not have a body and no Content Length
  166. * Status 304 does not have a body and does not need a Content Length
  167. * https://tools.ietf.org/html/rfc7230#section-3.3
  168. * https://tools.ietf.org/html/rfc7230#section-3.3.2
  169. */
  170. $emptyResponse = false;
  171. if (preg_match('/^HTTP\/\d\.\d (\d{3}) .*$/', $httpHeaders, $matches)) {
  172. $status = (int)$matches[1];
  173. if ($status === Http::STATUS_NO_CONTENT || $status === Http::STATUS_NOT_MODIFIED) {
  174. $emptyResponse = true;
  175. }
  176. }
  177. if (!$emptyResponse) {
  178. if ($response instanceof ICallbackResponse) {
  179. $response->callback($io);
  180. } elseif (!is_null($output)) {
  181. $io->setHeader('Content-Length: ' . strlen($output));
  182. $io->setOutput($output);
  183. }
  184. }
  185. }
  186. /**
  187. * Shortcut for calling a controller method and printing the result.
  188. * Similar to App:main except that no headers will be sent.
  189. * This should be used for example when registering sections via
  190. * \OC\AppFramework\Core\API::registerAdmin()
  191. *
  192. * @param string $controllerName the name of the controller under which it is
  193. * stored in the DI container
  194. * @param string $methodName the method that you want to call
  195. * @param array $urlParams an array with variables extracted from the routes
  196. * @param DIContainer $container an instance of a pimple container.
  197. */
  198. public static function part(string $controllerName, string $methodName, array $urlParams,
  199. DIContainer $container) {
  200. $container['urlParams'] = $urlParams;
  201. $controller = $container[$controllerName];
  202. $dispatcher = $container['Dispatcher'];
  203. [, , $output] = $dispatcher->dispatch($controller, $methodName);
  204. return $output;
  205. }
  206. }