ResourceLocator.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ <skjnldsv@protonmail.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Robin McCorkell <robin@mccorkell.me.uk>
  13. * @author tux-rampage <tux-rampage@users.noreply.github.com>
  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\Template;
  31. use Psr\Log\LoggerInterface;
  32. abstract class ResourceLocator {
  33. protected $theme;
  34. protected $mapping;
  35. protected $serverroot;
  36. protected $webroot;
  37. protected $resources = [];
  38. protected LoggerInterface $logger;
  39. public function __construct(LoggerInterface $logger) {
  40. $this->logger = $logger;
  41. $this->mapping = [
  42. \OC::$SERVERROOT => \OC::$WEBROOT
  43. ];
  44. $this->serverroot = \OC::$SERVERROOT;
  45. $this->webroot = \OC::$WEBROOT;
  46. $this->theme = \OC_Util::getTheme();
  47. }
  48. /**
  49. * @param string $resource
  50. */
  51. abstract public function doFind($resource);
  52. /**
  53. * @param string $resource
  54. */
  55. abstract public function doFindTheme($resource);
  56. /**
  57. * Finds the resources and adds them to the list
  58. *
  59. * @param array $resources
  60. */
  61. public function find($resources) {
  62. foreach ($resources as $resource) {
  63. try {
  64. $this->doFind($resource);
  65. } catch (ResourceNotFoundException $e) {
  66. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  67. $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  68. }
  69. }
  70. if (!empty($this->theme)) {
  71. foreach ($resources as $resource) {
  72. try {
  73. $this->doFindTheme($resource);
  74. } catch (ResourceNotFoundException $e) {
  75. $resourceApp = substr($resource, 0, strpos($resource, '/'));
  76. $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]);
  77. }
  78. }
  79. }
  80. }
  81. /**
  82. * append the $file resource if exist at $root
  83. *
  84. * @param string $root path to check
  85. * @param string $file the filename
  86. * @param string|null $webRoot base for path, default map $root to $webRoot
  87. * @return bool True if the resource was found, false otherwise
  88. */
  89. protected function appendIfExist($root, $file, $webRoot = null) {
  90. if ($root !== false && is_file($root.'/'.$file)) {
  91. $this->append($root, $file, $webRoot, false);
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Attempt to find the webRoot
  98. *
  99. * traverse the potential web roots upwards in the path
  100. *
  101. * example:
  102. * - root: /srv/www/apps/myapp
  103. * - available mappings: ['/srv/www']
  104. *
  105. * First we check if a mapping for /srv/www/apps/myapp is available,
  106. * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a
  107. * valid web root
  108. *
  109. * @param string $root
  110. * @return string|null The web root or null on failure
  111. */
  112. protected function findWebRoot($root) {
  113. $webRoot = null;
  114. $tmpRoot = $root;
  115. while ($webRoot === null) {
  116. if (isset($this->mapping[$tmpRoot])) {
  117. $webRoot = $this->mapping[$tmpRoot];
  118. break;
  119. }
  120. if ($tmpRoot === '/') {
  121. break;
  122. }
  123. $tmpRoot = dirname($tmpRoot);
  124. }
  125. if ($webRoot === null) {
  126. $realpath = realpath($root);
  127. if ($realpath && ($realpath !== $root)) {
  128. return $this->findWebRoot($realpath);
  129. }
  130. }
  131. return $webRoot;
  132. }
  133. /**
  134. * append the $file resource at $root
  135. *
  136. * @param string $root path to check
  137. * @param string $file the filename
  138. * @param string|null $webRoot base for path, default map $root to $webRoot
  139. * @param bool $throw Throw an exception, when the route does not exist
  140. * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing
  141. */
  142. protected function append($root, $file, $webRoot = null, $throw = true) {
  143. if (!is_string($root)) {
  144. if ($throw) {
  145. throw new ResourceNotFoundException($file, $webRoot);
  146. }
  147. return;
  148. }
  149. if (!$webRoot) {
  150. $webRoot = $this->findWebRoot($root);
  151. if ($webRoot === null) {
  152. $webRoot = '';
  153. $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [
  154. 'app' => 'lib',
  155. 'root' => $root,
  156. 'file' => $file,
  157. 'webRoot' => $webRoot,
  158. 'throw' => $throw ? 'true' : 'false'
  159. ]);
  160. }
  161. }
  162. $this->resources[] = [$root, $webRoot, $file];
  163. if ($throw && !is_file($root . '/' . $file)) {
  164. throw new ResourceNotFoundException($file, $webRoot);
  165. }
  166. }
  167. /**
  168. * Returns the list of all resources that should be loaded
  169. * @return array
  170. */
  171. public function getResources() {
  172. return $this->resources;
  173. }
  174. }