app.php 5.5 KB

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