1
0

URLGenerator.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OC;
  9. use OC\Route\Router;
  10. use OCA\Theming\ThemingDefaults;
  11. use OCP\App\AppPathNotFoundException;
  12. use OCP\App\IAppManager;
  13. use OCP\ICacheFactory;
  14. use OCP\IConfig;
  15. use OCP\IRequest;
  16. use OCP\IURLGenerator;
  17. use OCP\IUserSession;
  18. use RuntimeException;
  19. /**
  20. * Class to generate URLs
  21. */
  22. class URLGenerator implements IURLGenerator {
  23. /** @var IConfig */
  24. private $config;
  25. /** @var IUserSession */
  26. public $userSession;
  27. /** @var ICacheFactory */
  28. private $cacheFactory;
  29. /** @var IRequest */
  30. private $request;
  31. /** @var Router */
  32. private $router;
  33. /** @var null|string */
  34. private $baseUrl = null;
  35. private ?IAppManager $appManager = null;
  36. public function __construct(IConfig $config,
  37. IUserSession $userSession,
  38. ICacheFactory $cacheFactory,
  39. IRequest $request,
  40. Router $router
  41. ) {
  42. $this->config = $config;
  43. $this->userSession = $userSession;
  44. $this->cacheFactory = $cacheFactory;
  45. $this->request = $request;
  46. $this->router = $router;
  47. }
  48. private function getAppManager(): IAppManager {
  49. if ($this->appManager !== null) {
  50. return $this->appManager;
  51. }
  52. $this->appManager = \OCP\Server::get(IAppManager::class);
  53. return $this->appManager;
  54. }
  55. /**
  56. * Creates an url using a defined route
  57. *
  58. * @param string $routeName
  59. * @param array $arguments args with param=>value, will be appended to the returned url
  60. * @return string the url
  61. *
  62. * Returns a url to the given route.
  63. */
  64. public function linkToRoute(string $routeName, array $arguments = []): string {
  65. return $this->router->generate($routeName, $arguments);
  66. }
  67. /**
  68. * Creates an absolute url using a defined route
  69. * @param string $routeName
  70. * @param array $arguments args with param=>value, will be appended to the returned url
  71. * @return string the url
  72. *
  73. * Returns an absolute url to the given route.
  74. */
  75. public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
  76. return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
  77. }
  78. public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
  79. // Returns `/subfolder/index.php/ocsapp/…` with `'htaccess.IgnoreFrontController' => false` in config.php
  80. // And `/subfolder/ocsapp/…` with `'htaccess.IgnoreFrontController' => true` in config.php
  81. $route = $this->router->generate('ocs.'.$routeName, $arguments, false);
  82. // Cut off `/subfolder`
  83. if (\OC::$WEBROOT !== '' && str_starts_with($route, \OC::$WEBROOT)) {
  84. $route = substr($route, \strlen(\OC::$WEBROOT));
  85. }
  86. if (str_starts_with($route, '/index.php/')) {
  87. $route = substr($route, 10);
  88. }
  89. // Remove `ocsapp/` bit
  90. $route = substr($route, 7);
  91. // Prefix with ocs/v2.php endpoint
  92. $route = '/ocs/v2.php' . $route;
  93. // Turn into an absolute URL
  94. return $this->getAbsoluteURL($route);
  95. }
  96. /**
  97. * Creates an url
  98. *
  99. * @param string $appName app
  100. * @param string $file file
  101. * @param array $args array with param=>value, will be appended to the returned url
  102. * The value of $args will be urlencoded
  103. * @return string the url
  104. *
  105. * Returns a url to the given app and file.
  106. */
  107. public function linkTo(string $appName, string $file, array $args = []): string {
  108. $frontControllerActive = ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) || getenv('front_controller_active') === 'true');
  109. if ($appName !== '') {
  110. $app_path = $this->getAppManager()->getAppPath($appName);
  111. // Check if the app is in the app folder
  112. if (file_exists($app_path . '/' . $file)) {
  113. if (str_ends_with($file, 'php')) {
  114. $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName;
  115. if ($frontControllerActive) {
  116. $urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName;
  117. }
  118. $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
  119. } else {
  120. $urlLinkTo = $this->getAppManager()->getAppWebPath($appName) . '/' . $file;
  121. }
  122. } else {
  123. $urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file;
  124. }
  125. } else {
  126. if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
  127. $urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
  128. } else {
  129. if ($frontControllerActive && $file === 'index.php') {
  130. $urlLinkTo = \OC::$WEBROOT . '/';
  131. } else {
  132. $urlLinkTo = \OC::$WEBROOT . '/' . $file;
  133. }
  134. }
  135. }
  136. if ($args && $query = http_build_query($args, '', '&')) {
  137. $urlLinkTo .= '?' . $query;
  138. }
  139. return $urlLinkTo;
  140. }
  141. /**
  142. * Creates path to an image
  143. *
  144. * @param string $appName app
  145. * @param string $file image name
  146. * @throws \RuntimeException If the image does not exist
  147. * @return string the url
  148. *
  149. * Returns the path to the image.
  150. */
  151. public function imagePath(string $appName, string $file): string {
  152. $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
  153. $cacheKey = $appName.'-'.$file;
  154. if ($key = $cache->get($cacheKey)) {
  155. return $key;
  156. }
  157. // Read the selected theme from the config file
  158. $theme = \OC_Util::getTheme();
  159. //if a theme has a png but not an svg always use the png
  160. $basename = substr(basename($file), 0, -4);
  161. try {
  162. $appPath = $this->getAppManager()->getAppPath($appName);
  163. } catch (AppPathNotFoundException $e) {
  164. if ($appName === 'core' || $appName === '') {
  165. $appName = 'core';
  166. $appPath = false;
  167. } else {
  168. throw new RuntimeException('image not found: image: ' . $file . ' webroot: ' . \OC::$WEBROOT . ' serverroot: ' . \OC::$SERVERROOT);
  169. }
  170. }
  171. // Check if the app is in the app folder
  172. $path = '';
  173. $themingEnabled = $this->config->getSystemValueBool('installed', false) && $this->getAppManager()->isEnabledForUser('theming');
  174. $themingImagePath = false;
  175. if ($themingEnabled) {
  176. $themingDefaults = \OC::$server->get('ThemingDefaults');
  177. if ($themingDefaults instanceof ThemingDefaults) {
  178. $themingImagePath = $themingDefaults->replaceImagePath($appName, $file);
  179. }
  180. }
  181. if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) {
  182. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file";
  183. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg")
  184. && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) {
  185. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png";
  186. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) {
  187. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file";
  188. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg")
  189. && file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) {
  190. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png";
  191. } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) {
  192. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$file";
  193. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
  194. && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
  195. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  196. } elseif ($themingEnabled && $themingImagePath) {
  197. $path = $themingImagePath;
  198. } elseif ($appPath && file_exists($appPath . "/img/$file")) {
  199. $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$file";
  200. } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
  201. && file_exists($appPath . "/img/$basename.png")) {
  202. $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$basename.png";
  203. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) {
  204. $path = \OC::$WEBROOT . "/$appName/img/$file";
  205. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg")
  206. && file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) {
  207. $path = \OC::$WEBROOT . "/$appName/img/$basename.png";
  208. } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) {
  209. $path = \OC::$WEBROOT . "/core/img/$file";
  210. } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg")
  211. && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) {
  212. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  213. }
  214. if ($path !== '') {
  215. $cache->set($cacheKey, $path);
  216. return $path;
  217. }
  218. throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
  219. }
  220. /**
  221. * Makes an URL absolute
  222. * @param string $url the url in the ownCloud host
  223. * @return string the absolute version of the url
  224. */
  225. public function getAbsoluteURL(string $url): string {
  226. $separator = str_starts_with($url, '/') ? '' : '/';
  227. if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
  228. return rtrim($this->config->getSystemValueString('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
  229. }
  230. // The ownCloud web root can already be prepended.
  231. if (\OC::$WEBROOT !== '' && str_starts_with($url, \OC::$WEBROOT)) {
  232. $url = substr($url, \strlen(\OC::$WEBROOT));
  233. }
  234. return $this->getBaseUrl() . $separator . $url;
  235. }
  236. /**
  237. * @param string $key
  238. * @return string url to the online documentation
  239. */
  240. public function linkToDocs(string $key): string {
  241. $theme = \OC::$server->get('ThemingDefaults');
  242. return $theme->buildDocLinkToKey($key);
  243. }
  244. /**
  245. * Returns the URL of the default page based on the system configuration
  246. * and the apps visible for the current user
  247. * @return string
  248. */
  249. public function linkToDefaultPageUrl(): string {
  250. // Deny the redirect if the URL contains a @
  251. // This prevents unvalidated redirects like ?redirect_url=:user@domain.com
  252. if (isset($_REQUEST['redirect_url']) && !str_contains($_REQUEST['redirect_url'], '@')) {
  253. return $this->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
  254. }
  255. $defaultPage = $this->config->getAppValue('core', 'defaultpage');
  256. if ($defaultPage) {
  257. return $this->getAbsoluteURL($defaultPage);
  258. }
  259. $appId = $this->getAppManager()->getDefaultAppForUser();
  260. if ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false)
  261. || getenv('front_controller_active') === 'true') {
  262. return $this->getAbsoluteURL('/apps/' . $appId . '/');
  263. }
  264. return $this->getAbsoluteURL('/index.php/apps/' . $appId . '/');
  265. }
  266. /**
  267. * @return string base url of the current request
  268. */
  269. public function getBaseUrl(): string {
  270. // BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup.
  271. if ($this->baseUrl === null || $this->baseUrl === "http://" || $this->baseUrl === "https://") {
  272. $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
  273. }
  274. return $this->baseUrl;
  275. }
  276. /**
  277. * @return string webroot part of the base url
  278. */
  279. public function getWebroot(): string {
  280. return \OC::$WEBROOT;
  281. }
  282. }