Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

URLGenerator.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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\ICacheFactory;
  43. use OCP\IConfig;
  44. use OCP\IRequest;
  45. use OCP\IURLGenerator;
  46. use OCP\IUserSession;
  47. use RuntimeException;
  48. /**
  49. * Class to generate URLs
  50. */
  51. class URLGenerator implements IURLGenerator {
  52. /** @var IConfig */
  53. private $config;
  54. /** @var IUserSession */
  55. public $userSession;
  56. /** @var ICacheFactory */
  57. private $cacheFactory;
  58. /** @var IRequest */
  59. private $request;
  60. /** @var Router */
  61. private $router;
  62. /** @var null|string */
  63. private $baseUrl = null;
  64. public function __construct(IConfig $config,
  65. IUserSession $userSession,
  66. ICacheFactory $cacheFactory,
  67. IRequest $request,
  68. Router $router) {
  69. $this->config = $config;
  70. $this->userSession = $userSession;
  71. $this->cacheFactory = $cacheFactory;
  72. $this->request = $request;
  73. $this->router = $router;
  74. }
  75. /**
  76. * Creates an url using a defined route
  77. *
  78. * @param string $routeName
  79. * @param array $arguments args with param=>value, will be appended to the returned url
  80. * @return string the url
  81. *
  82. * Returns a url to the given route.
  83. */
  84. public function linkToRoute(string $routeName, array $arguments = []): string {
  85. return $this->router->generate($routeName, $arguments);
  86. }
  87. /**
  88. * Creates an absolute url using a defined route
  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 an absolute url to the given route.
  94. */
  95. public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
  96. return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
  97. }
  98. public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
  99. $route = $this->router->generate('ocs.'.$routeName, $arguments, false);
  100. $indexPhpPos = strpos($route, '/index.php/');
  101. if ($indexPhpPos !== false) {
  102. $route = substr($route, $indexPhpPos + 10);
  103. }
  104. $route = substr($route, 7);
  105. $route = '/ocs/v2.php' . $route;
  106. return $this->getAbsoluteURL($route);
  107. }
  108. /**
  109. * Creates an url
  110. *
  111. * @param string $appName app
  112. * @param string $file file
  113. * @param array $args array with param=>value, will be appended to the returned url
  114. * The value of $args will be urlencoded
  115. * @return string the url
  116. *
  117. * Returns a url to the given app and file.
  118. */
  119. public function linkTo(string $appName, string $file, array $args = []): string {
  120. $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
  121. if ($appName !== '') {
  122. $app_path = \OC_App::getAppPath($appName);
  123. // Check if the app is in the app folder
  124. if ($app_path && file_exists($app_path . '/' . $file)) {
  125. if (substr($file, -3) === 'php') {
  126. $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName;
  127. if ($frontControllerActive) {
  128. $urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName;
  129. }
  130. $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
  131. } else {
  132. $urlLinkTo = \OC_App::getAppWebPath($appName) . '/' . $file;
  133. }
  134. } else {
  135. $urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file;
  136. }
  137. } else {
  138. if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
  139. $urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
  140. } else {
  141. if ($frontControllerActive && $file === 'index.php') {
  142. $urlLinkTo = \OC::$WEBROOT . '/';
  143. } else {
  144. $urlLinkTo = \OC::$WEBROOT . '/' . $file;
  145. }
  146. }
  147. }
  148. if ($args && $query = http_build_query($args, '', '&')) {
  149. $urlLinkTo .= '?' . $query;
  150. }
  151. return $urlLinkTo;
  152. }
  153. /**
  154. * Creates path to an image
  155. *
  156. * @param string $appName app
  157. * @param string $file image name
  158. * @throws \RuntimeException If the image does not exist
  159. * @return string the url
  160. *
  161. * Returns the path to the image.
  162. */
  163. public function imagePath(string $appName, string $file): string {
  164. $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
  165. $cacheKey = $appName.'-'.$file;
  166. if ($key = $cache->get($cacheKey)) {
  167. return $key;
  168. }
  169. // Read the selected theme from the config file
  170. $theme = \OC_Util::getTheme();
  171. //if a theme has a png but not an svg always use the png
  172. $basename = substr(basename($file),0,-4);
  173. $appPath = \OC_App::getAppPath($appName);
  174. // Check if the app is in the app folder
  175. $path = '';
  176. $themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming');
  177. $themingImagePath = false;
  178. if ($themingEnabled) {
  179. $themingDefaults = \OC::$server->getThemingDefaults();
  180. if ($themingDefaults instanceof ThemingDefaults) {
  181. $themingImagePath = $themingDefaults->replaceImagePath($appName, $file);
  182. }
  183. }
  184. if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) {
  185. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file";
  186. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg")
  187. && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) {
  188. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png";
  189. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) {
  190. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file";
  191. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg")
  192. && file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) {
  193. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png";
  194. } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) {
  195. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$file";
  196. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
  197. && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
  198. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  199. } elseif ($themingEnabled && $themingImagePath) {
  200. $path = $themingImagePath;
  201. } elseif ($appPath && file_exists($appPath . "/img/$file")) {
  202. $path = \OC_App::getAppWebPath($appName) . "/img/$file";
  203. } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
  204. && file_exists($appPath . "/img/$basename.png")) {
  205. $path = \OC_App::getAppWebPath($appName) . "/img/$basename.png";
  206. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) {
  207. $path = \OC::$WEBROOT . "/$appName/img/$file";
  208. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg")
  209. && file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) {
  210. $path = \OC::$WEBROOT . "/$appName/img/$basename.png";
  211. } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) {
  212. $path = \OC::$WEBROOT . "/core/img/$file";
  213. } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg")
  214. && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) {
  215. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  216. }
  217. if ($path !== '') {
  218. $cache->set($cacheKey, $path);
  219. return $path;
  220. }
  221. throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
  222. }
  223. /**
  224. * Makes an URL absolute
  225. * @param string $url the url in the ownCloud host
  226. * @return string the absolute version of the url
  227. */
  228. public function getAbsoluteURL(string $url): string {
  229. $separator = strpos($url, '/') === 0 ? '' : '/';
  230. if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
  231. return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
  232. }
  233. // The ownCloud web root can already be prepended.
  234. if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) {
  235. $url = substr($url, \strlen(\OC::$WEBROOT));
  236. }
  237. return $this->getBaseUrl() . $separator . $url;
  238. }
  239. /**
  240. * @param string $key
  241. * @return string url to the online documentation
  242. */
  243. public function linkToDocs(string $key): string {
  244. $theme = \OC::$server->getThemingDefaults();
  245. return $theme->buildDocLinkToKey($key);
  246. }
  247. /**
  248. * Returns the URL of the default page based on the system configuration
  249. * and the apps visible for the current user
  250. * @return string
  251. */
  252. public function linkToDefaultPageUrl(): string {
  253. // Deny the redirect if the URL contains a @
  254. // This prevents unvalidated redirects like ?redirect_url=:user@domain.com
  255. if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) {
  256. return $this->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
  257. }
  258. $defaultPage = $this->config->getAppValue('core', 'defaultpage');
  259. if ($defaultPage) {
  260. return $this->getAbsoluteURL($defaultPage);
  261. }
  262. $appId = 'files';
  263. $defaultApps = explode(',', $this->config->getSystemValue('defaultapp', 'dashboard,files'));
  264. $userId = $this->userSession->isLoggedIn() ? $this->userSession->getUser()->getUID() : null;
  265. if ($userId !== null) {
  266. $userDefaultApps = explode(',', $this->config->getUserValue($userId, 'core', 'defaultapp'));
  267. $defaultApps = array_filter(array_merge($userDefaultApps, $defaultApps));
  268. }
  269. // find the first app that is enabled for the current user
  270. foreach ($defaultApps as $defaultApp) {
  271. $defaultApp = \OC_App::cleanAppId(strip_tags($defaultApp));
  272. if (\OC::$server->getAppManager()->isEnabledForUser($defaultApp)) {
  273. $appId = $defaultApp;
  274. break;
  275. }
  276. }
  277. if ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true
  278. || getenv('front_controller_active') === 'true') {
  279. return $this->getAbsoluteURL('/apps/' . $appId . '/');
  280. }
  281. return $this->getAbsoluteURL('/index.php/apps/' . $appId . '/');
  282. }
  283. /**
  284. * @return string base url of the current request
  285. */
  286. public function getBaseUrl(): string {
  287. // BaseUrl can be equal to 'http(s)://' during the first steps of the intial setup.
  288. if ($this->baseUrl === null || $this->baseUrl === "http://" || $this->baseUrl === "https://") {
  289. $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
  290. }
  291. return $this->baseUrl;
  292. }
  293. /**
  294. * @return string webroot part of the base url
  295. */
  296. public function getWebroot(): string {
  297. return \OC::$WEBROOT;
  298. }
  299. }