URLGenerator.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Bart Visscher <bartv@thisnet.nl>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Rudolf <github.com@daniel-rudolf.de>
  10. * @author Felix Epp <work@felixepp.de>
  11. * @author Joas Schilling <coding@schilljs.com>
  12. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  13. * @author Julius Haertl <jus@bitgrid.net>
  14. * @author Julius Härtl <jus@bitgrid.net>
  15. * @author Lukas Reschke <lukas@statuscode.ch>
  16. * @author mmccarn <mmccarn-github@mmsionline.us>
  17. * @author Morris Jobke <hey@morrisjobke.de>
  18. * @author Robin Appelman <robin@icewind.nl>
  19. * @author Robin McCorkell <robin@mccorkell.me.uk>
  20. * @author Roeland Jago Douma <roeland@famdouma.nl>
  21. * @author Thomas Müller <thomas.mueller@tmit.eu>
  22. * @author Thomas Tanghus <thomas@tanghus.net>
  23. *
  24. * @license AGPL-3.0
  25. *
  26. * This code is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Affero General Public License, version 3,
  28. * as published by the Free Software Foundation.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU Affero General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Affero General Public License, version 3,
  36. * along with this program. If not, see <http://www.gnu.org/licenses/>
  37. *
  38. */
  39. namespace OC;
  40. use OC\Route\Router;
  41. use OCA\Theming\ThemingDefaults;
  42. use OCP\App\AppPathNotFoundException;
  43. use OCP\App\IAppManager;
  44. use OCP\ICacheFactory;
  45. use OCP\IConfig;
  46. use OCP\IRequest;
  47. use OCP\IURLGenerator;
  48. use OCP\IUserSession;
  49. use RuntimeException;
  50. /**
  51. * Class to generate URLs
  52. */
  53. class URLGenerator implements IURLGenerator {
  54. /** @var IConfig */
  55. private $config;
  56. /** @var IUserSession */
  57. public $userSession;
  58. /** @var ICacheFactory */
  59. private $cacheFactory;
  60. /** @var IRequest */
  61. private $request;
  62. /** @var Router */
  63. private $router;
  64. /** @var null|string */
  65. private $baseUrl = null;
  66. private ?IAppManager $appManager = null;
  67. public function __construct(IConfig $config,
  68. IUserSession $userSession,
  69. ICacheFactory $cacheFactory,
  70. IRequest $request,
  71. Router $router
  72. ) {
  73. $this->config = $config;
  74. $this->userSession = $userSession;
  75. $this->cacheFactory = $cacheFactory;
  76. $this->request = $request;
  77. $this->router = $router;
  78. }
  79. private function getAppManager(): IAppManager {
  80. if ($this->appManager !== null) {
  81. return $this->appManager;
  82. }
  83. $this->appManager = \OCP\Server::get(IAppManager::class);
  84. return $this->appManager;
  85. }
  86. /**
  87. * Creates an url using a defined route
  88. *
  89. * @param string $routeName
  90. * @param array $arguments args with param=>value, will be appended to the returned url
  91. * @return string the url
  92. *
  93. * Returns a url to the given route.
  94. */
  95. public function linkToRoute(string $routeName, array $arguments = []): string {
  96. return $this->router->generate($routeName, $arguments);
  97. }
  98. /**
  99. * Creates an absolute url using a defined route
  100. * @param string $routeName
  101. * @param array $arguments args with param=>value, will be appended to the returned url
  102. * @return string the url
  103. *
  104. * Returns an absolute url to the given route.
  105. */
  106. public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
  107. return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
  108. }
  109. public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
  110. // Returns `/subfolder/index.php/ocsapp/…` with `'htaccess.IgnoreFrontController' => false` in config.php
  111. // And `/subfolder/ocsapp/…` with `'htaccess.IgnoreFrontController' => true` in config.php
  112. $route = $this->router->generate('ocs.'.$routeName, $arguments, false);
  113. // Cut off `/subfolder`
  114. if (\OC::$WEBROOT !== '' && str_starts_with($route, \OC::$WEBROOT)) {
  115. $route = substr($route, \strlen(\OC::$WEBROOT));
  116. }
  117. if (str_starts_with($route, '/index.php/')) {
  118. $route = substr($route, 10);
  119. }
  120. // Remove `ocsapp/` bit
  121. $route = substr($route, 7);
  122. // Prefix with ocs/v2.php endpoint
  123. $route = '/ocs/v2.php' . $route;
  124. // Turn into an absolute URL
  125. return $this->getAbsoluteURL($route);
  126. }
  127. /**
  128. * Creates an url
  129. *
  130. * @param string $appName app
  131. * @param string $file file
  132. * @param array $args array with param=>value, will be appended to the returned url
  133. * The value of $args will be urlencoded
  134. * @return string the url
  135. *
  136. * Returns a url to the given app and file.
  137. */
  138. public function linkTo(string $appName, string $file, array $args = []): string {
  139. $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
  140. if ($appName !== '') {
  141. $app_path = $this->getAppManager()->getAppPath($appName);
  142. // Check if the app is in the app folder
  143. if (file_exists($app_path . '/' . $file)) {
  144. if (substr($file, -3) === 'php') {
  145. $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName;
  146. if ($frontControllerActive) {
  147. $urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName;
  148. }
  149. $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
  150. } else {
  151. $urlLinkTo = $this->getAppManager()->getAppWebPath($appName) . '/' . $file;
  152. }
  153. } else {
  154. $urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file;
  155. }
  156. } else {
  157. if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
  158. $urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
  159. } else {
  160. if ($frontControllerActive && $file === 'index.php') {
  161. $urlLinkTo = \OC::$WEBROOT . '/';
  162. } else {
  163. $urlLinkTo = \OC::$WEBROOT . '/' . $file;
  164. }
  165. }
  166. }
  167. if ($args && $query = http_build_query($args, '', '&')) {
  168. $urlLinkTo .= '?' . $query;
  169. }
  170. return $urlLinkTo;
  171. }
  172. /**
  173. * Creates path to an image
  174. *
  175. * @param string $appName app
  176. * @param string $file image name
  177. * @throws \RuntimeException If the image does not exist
  178. * @return string the url
  179. *
  180. * Returns the path to the image.
  181. */
  182. public function imagePath(string $appName, string $file): string {
  183. $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
  184. $cacheKey = $appName.'-'.$file;
  185. if ($key = $cache->get($cacheKey)) {
  186. return $key;
  187. }
  188. // Read the selected theme from the config file
  189. $theme = \OC_Util::getTheme();
  190. //if a theme has a png but not an svg always use the png
  191. $basename = substr(basename($file), 0, -4);
  192. try {
  193. $appPath = $this->getAppManager()->getAppPath($appName);
  194. } catch (AppPathNotFoundException $e) {
  195. if ($appName === 'core' || $appName === '') {
  196. $appName = 'core';
  197. $appPath = false;
  198. } else {
  199. throw new RuntimeException('image not found: image: ' . $file . ' webroot: ' . \OC::$WEBROOT . ' serverroot: ' . \OC::$SERVERROOT);
  200. }
  201. }
  202. // Check if the app is in the app folder
  203. $path = '';
  204. $themingEnabled = $this->config->getSystemValue('installed', false) && $this->getAppManager()->isEnabledForUser('theming');
  205. $themingImagePath = false;
  206. if ($themingEnabled) {
  207. $themingDefaults = \OC::$server->getThemingDefaults();
  208. if ($themingDefaults instanceof ThemingDefaults) {
  209. $themingImagePath = $themingDefaults->replaceImagePath($appName, $file);
  210. }
  211. }
  212. if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) {
  213. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file";
  214. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg")
  215. && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) {
  216. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png";
  217. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) {
  218. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file";
  219. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg")
  220. && file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) {
  221. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png";
  222. } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) {
  223. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$file";
  224. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
  225. && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
  226. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  227. } elseif ($themingEnabled && $themingImagePath) {
  228. $path = $themingImagePath;
  229. } elseif ($appPath && file_exists($appPath . "/img/$file")) {
  230. $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$file";
  231. } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
  232. && file_exists($appPath . "/img/$basename.png")) {
  233. $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$basename.png";
  234. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) {
  235. $path = \OC::$WEBROOT . "/$appName/img/$file";
  236. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg")
  237. && file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) {
  238. $path = \OC::$WEBROOT . "/$appName/img/$basename.png";
  239. } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) {
  240. $path = \OC::$WEBROOT . "/core/img/$file";
  241. } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg")
  242. && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) {
  243. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  244. }
  245. if ($path !== '') {
  246. $cache->set($cacheKey, $path);
  247. return $path;
  248. }
  249. throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
  250. }
  251. /**
  252. * Makes an URL absolute
  253. * @param string $url the url in the ownCloud host
  254. * @return string the absolute version of the url
  255. */
  256. public function getAbsoluteURL(string $url): string {
  257. $separator = strpos($url, '/') === 0 ? '' : '/';
  258. if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
  259. return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
  260. }
  261. // The ownCloud web root can already be prepended.
  262. if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) {
  263. $url = substr($url, \strlen(\OC::$WEBROOT));
  264. }
  265. return $this->getBaseUrl() . $separator . $url;
  266. }
  267. /**
  268. * @param string $key
  269. * @return string url to the online documentation
  270. */
  271. public function linkToDocs(string $key): string {
  272. $theme = \OC::$server->getThemingDefaults();
  273. return $theme->buildDocLinkToKey($key);
  274. }
  275. /**
  276. * Returns the URL of the default page based on the system configuration
  277. * and the apps visible for the current user
  278. * @return string
  279. */
  280. public function linkToDefaultPageUrl(): string {
  281. // Deny the redirect if the URL contains a @
  282. // This prevents unvalidated redirects like ?redirect_url=:user@domain.com
  283. if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) {
  284. return $this->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
  285. }
  286. $defaultPage = $this->config->getAppValue('core', 'defaultpage');
  287. if ($defaultPage) {
  288. return $this->getAbsoluteURL($defaultPage);
  289. }
  290. $appId = $this->getAppManager()->getDefaultAppForUser();
  291. if ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true
  292. || getenv('front_controller_active') === 'true') {
  293. return $this->getAbsoluteURL('/apps/' . $appId . '/');
  294. }
  295. return $this->getAbsoluteURL('/index.php/apps/' . $appId . '/');
  296. }
  297. /**
  298. * @return string base url of the current request
  299. */
  300. public function getBaseUrl(): string {
  301. // BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup.
  302. if ($this->baseUrl === null || $this->baseUrl === "http://" || $this->baseUrl === "https://") {
  303. $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
  304. }
  305. return $this->baseUrl;
  306. }
  307. /**
  308. * @return string webroot part of the base url
  309. */
  310. public function getWebroot(): string {
  311. return \OC::$WEBROOT;
  312. }
  313. }