ResourceLocator.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Template;
  8. use Psr\Log\LoggerInterface;
  9. abstract class ResourceLocator {
  10. protected $theme;
  11. protected $mapping;
  12. protected $serverroot;
  13. protected $webroot;
  14. protected $resources = [];
  15. protected LoggerInterface $logger;
  16. public function __construct(LoggerInterface $logger) {
  17. $this->logger = $logger;
  18. $this->mapping = [
  19. \OC::$SERVERROOT => \OC::$WEBROOT
  20. ];
  21. $this->serverroot = \OC::$SERVERROOT;
  22. $this->webroot = \OC::$WEBROOT;
  23. $this->theme = \OC_Util::getTheme();
  24. }
  25. /**
  26. * @param string $resource
  27. */
  28. abstract public function doFind($resource);
  29. /**
  30. * @param string $resource
  31. */
  32. abstract public function doFindTheme($resource);
  33. /**
  34. * Finds the resources and adds them to the list
  35. *
  36. * @param array $resources
  37. */
  38. public function find($resources) {
  39. foreach ($resources as $resource) {
  40. try {
  41. $this->doFind($resource);
  42. } catch (ResourceNotFoundException $e) {
  43. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  44. $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  45. }
  46. }
  47. if (!empty($this->theme)) {
  48. foreach ($resources as $resource) {
  49. try {
  50. $this->doFindTheme($resource);
  51. } catch (ResourceNotFoundException $e) {
  52. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  53. $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  54. }
  55. }
  56. }
  57. }
  58. /**
  59. * append the $file resource if exist at $root
  60. *
  61. * @param string $root path to check
  62. * @param string $file the filename
  63. * @param string|null $webRoot base for path, default map $root to $webRoot
  64. * @return bool True if the resource was found, false otherwise
  65. */
  66. protected function appendIfExist($root, $file, $webRoot = null) {
  67. if ($root !== false && is_file($root.'/'.$file)) {
  68. $this->append($root, $file, $webRoot, false);
  69. return true;
  70. }
  71. return false;
  72. }
  73. /**
  74. * Attempt to find the webRoot
  75. *
  76. * traverse the potential web roots upwards in the path
  77. *
  78. * example:
  79. * - root: /srv/www/apps/myapp
  80. * - available mappings: ['/srv/www']
  81. *
  82. * First we check if a mapping for /srv/www/apps/myapp is available,
  83. * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
  84. * valid web root
  85. *
  86. * @param string $root
  87. * @return string|null The web root or null on failure
  88. */
  89. protected function findWebRoot($root) {
  90. $webRoot = null;
  91. $tmpRoot = $root;
  92. while ($webRoot === null) {
  93. if (isset($this->mapping[$tmpRoot])) {
  94. $webRoot = $this->mapping[$tmpRoot];
  95. break;
  96. }
  97. if ($tmpRoot === '/') {
  98. break;
  99. }
  100. $tmpRoot = dirname($tmpRoot);
  101. }
  102. if ($webRoot === null) {
  103. $realpath = realpath($root);
  104. if ($realpath && ($realpath !== $root)) {
  105. return $this->findWebRoot($realpath);
  106. }
  107. }
  108. return $webRoot;
  109. }
  110. /**
  111. * append the $file resource at $root
  112. *
  113. * @param string $root path to check
  114. * @param string $file the filename
  115. * @param string|null $webRoot base for path, default map $root to $webRoot
  116. * @param bool $throw Throw an exception, when the route does not exist
  117. * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
  118. */
  119. protected function append($root, $file, $webRoot = null, $throw = true) {
  120. if (!is_string($root)) {
  121. if ($throw) {
  122. throw new ResourceNotFoundException($file, $webRoot);
  123. }
  124. return;
  125. }
  126. if (!$webRoot) {
  127. $webRoot = $this->findWebRoot($root);
  128. if ($webRoot === null) {
  129. $webRoot = '';
  130. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  131. 'app' => 'lib',
  132. 'root' => $root,
  133. 'file' => $file,
  134. 'webRoot' => $webRoot,
  135. 'throw' => $throw ? 'true' : 'false'
  136. ]);
  137. }
  138. }
  139. $this->resources[] = [$root, $webRoot, $file];
  140. if ($throw && !is_file($root . '/' . $file)) {
  141. throw new ResourceNotFoundException($file, $webRoot);
  142. }
  143. }
  144. /**
  145. * Returns the list of all resources that should be loaded
  146. * @return array
  147. */
  148. public function getResources() {
  149. return $this->resources;
  150. }
  151. }