App.php 6.0 KB

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