1
0

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. self::$nameSpaceCache[$appId] = trim($appInfo['namespace']);
  58. } else {
  59. // if the tag is not found, fall back to uppercasing the first letter
  60. self::$nameSpaceCache[$appId] = ucfirst($appId);
  61. }
  62. return $topNamespace . self::$nameSpaceCache[$appId];
  63. }
  64. /**
  65. * Shortcut for calling a controller method and printing the result
  66. * @param string $controllerName the name of the controller under which it is
  67. * stored in the DI container
  68. * @param string $methodName the method that you want to call
  69. * @param DIContainer $container an instance of a pimple container.
  70. * @param array $urlParams list of URL parameters (optional)
  71. */
  72. public static function main($controllerName, $methodName, DIContainer $container, array $urlParams = null) {
  73. if (!is_null($urlParams)) {
  74. $container['OCP\\IRequest']->setUrlParameters($urlParams);
  75. } else if (isset($container['urlParams']) && !is_null($container['urlParams'])) {
  76. $container['OCP\\IRequest']->setUrlParameters($container['urlParams']);
  77. }
  78. $appName = $container['AppName'];
  79. // first try $controllerName then go for \OCA\AppName\Controller\$controllerName
  80. try {
  81. $controller = $container->query($controllerName);
  82. } catch(QueryException $e) {
  83. if ($appName === 'core') {
  84. $appNameSpace = 'OC\\Core';
  85. } else if ($appName === 'settings') {
  86. $appNameSpace = 'OC\\Settings';
  87. } else {
  88. $appNameSpace = self::buildAppNamespace($appName);
  89. }
  90. $controllerName = $appNameSpace . '\\Controller\\' . $controllerName;
  91. $controller = $container->query($controllerName);
  92. }
  93. // initialize the dispatcher and run all the middleware before the controller
  94. /** @var Dispatcher $dispatcher */
  95. $dispatcher = $container['Dispatcher'];
  96. list(
  97. $httpHeaders,
  98. $responseHeaders,
  99. $responseCookies,
  100. $output,
  101. $response
  102. ) = $dispatcher->dispatch($controller, $methodName);
  103. $io = $container['OCP\\AppFramework\\Http\\IOutput'];
  104. if(!is_null($httpHeaders)) {
  105. $io->setHeader($httpHeaders);
  106. }
  107. foreach($responseHeaders as $name => $value) {
  108. $io->setHeader($name . ': ' . $value);
  109. }
  110. foreach($responseCookies as $name => $value) {
  111. $expireDate = null;
  112. if($value['expireDate'] instanceof \DateTime) {
  113. $expireDate = $value['expireDate']->getTimestamp();
  114. }
  115. $io->setCookie(
  116. $name,
  117. $value['value'],
  118. $expireDate,
  119. $container->getServer()->getWebRoot(),
  120. null,
  121. $container->getServer()->getRequest()->getServerProtocol() === 'https',
  122. true
  123. );
  124. }
  125. /*
  126. * Status 204 does not have a body and no Content Length
  127. * Status 304 does not have a body and does not need a Content Length
  128. * https://tools.ietf.org/html/rfc7230#section-3.3
  129. * https://tools.ietf.org/html/rfc7230#section-3.3.2
  130. */
  131. if ($httpHeaders !== Http::STATUS_NO_CONTENT && $httpHeaders !== Http::STATUS_NOT_MODIFIED) {
  132. if ($response instanceof ICallbackResponse) {
  133. $response->callback($io);
  134. } else if (!is_null($output)) {
  135. $io->setHeader('Content-Length: ' . strlen($output));
  136. $io->setOutput($output);
  137. }
  138. }
  139. }
  140. /**
  141. * Shortcut for calling a controller method and printing the result.
  142. * Similar to App:main except that no headers will be sent.
  143. * This should be used for example when registering sections via
  144. * \OC\AppFramework\Core\API::registerAdmin()
  145. *
  146. * @param string $controllerName the name of the controller under which it is
  147. * stored in the DI container
  148. * @param string $methodName the method that you want to call
  149. * @param array $urlParams an array with variables extracted from the routes
  150. * @param DIContainer $container an instance of a pimple container.
  151. */
  152. public static function part($controllerName, $methodName, array $urlParams,
  153. DIContainer $container){
  154. $container['urlParams'] = $urlParams;
  155. $controller = $container[$controllerName];
  156. $dispatcher = $container['Dispatcher'];
  157. list(, , $output) = $dispatcher->dispatch($controller, $methodName);
  158. return $output;
  159. }
  160. }