You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URLGenerator.php 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 Felix Epp <work@felixepp.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Julius Haertl <jus@bitgrid.net>
  13. * @author Julius Härtl <jus@bitgrid.net>
  14. * @author Lukas Reschke <lukas@statuscode.ch>
  15. * @author mmccarn <mmccarn-github@mmsionline.us>
  16. * @author Morris Jobke <hey@morrisjobke.de>
  17. * @author Robin Appelman <robin@icewind.nl>
  18. * @author Robin McCorkell <robin@mccorkell.me.uk>
  19. * @author Roeland Jago Douma <roeland@famdouma.nl>
  20. * @author Thomas Müller <thomas.mueller@tmit.eu>
  21. * @author Thomas Tanghus <thomas@tanghus.net>
  22. *
  23. * @license AGPL-3.0
  24. *
  25. * This code is free software: you can redistribute it and/or modify
  26. * it under the terms of the GNU Affero General Public License, version 3,
  27. * as published by the Free Software Foundation.
  28. *
  29. * This program is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Affero General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Affero General Public License, version 3,
  35. * along with this program. If not, see <http://www.gnu.org/licenses/>
  36. *
  37. */
  38. namespace OC;
  39. use OC\Route\Router;
  40. use OCA\Theming\ThemingDefaults;
  41. use OCP\ICacheFactory;
  42. use OCP\IConfig;
  43. use OCP\IRequest;
  44. use OCP\IURLGenerator;
  45. use RuntimeException;
  46. /**
  47. * Class to generate URLs
  48. */
  49. class URLGenerator implements IURLGenerator {
  50. /** @var IConfig */
  51. private $config;
  52. /** @var ICacheFactory */
  53. private $cacheFactory;
  54. /** @var IRequest */
  55. private $request;
  56. /** @var Router */
  57. private $router;
  58. /** @var null|string */
  59. private $baseUrl = null;
  60. public function __construct(IConfig $config,
  61. ICacheFactory $cacheFactory,
  62. IRequest $request,
  63. Router $router) {
  64. $this->config = $config;
  65. $this->cacheFactory = $cacheFactory;
  66. $this->request = $request;
  67. $this->router = $router;
  68. }
  69. /**
  70. * Creates an url using a defined route
  71. *
  72. * @param string $routeName
  73. * @param array $arguments args with param=>value, will be appended to the returned url
  74. * @return string the url
  75. *
  76. * Returns a url to the given route.
  77. */
  78. public function linkToRoute(string $routeName, array $arguments = []): string {
  79. return $this->router->generate($routeName, $arguments);
  80. }
  81. /**
  82. * Creates an absolute url using a defined route
  83. * @param string $routeName
  84. * @param array $arguments args with param=>value, will be appended to the returned url
  85. * @return string the url
  86. *
  87. * Returns an absolute url to the given route.
  88. */
  89. public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
  90. return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
  91. }
  92. public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
  93. $route = $this->router->generate('ocs.'.$routeName, $arguments, false);
  94. $indexPhpPos = strpos($route, '/index.php/');
  95. if ($indexPhpPos !== false) {
  96. $route = substr($route, $indexPhpPos + 10);
  97. }
  98. $route = substr($route, 7);
  99. $route = '/ocs/v2.php' . $route;
  100. return $this->getAbsoluteURL($route);
  101. }
  102. /**
  103. * Creates an url
  104. *
  105. * @param string $appName app
  106. * @param string $file file
  107. * @param array $args array with param=>value, will be appended to the returned url
  108. * The value of $args will be urlencoded
  109. * @return string the url
  110. *
  111. * Returns a url to the given app and file.
  112. */
  113. public function linkTo(string $appName, string $file, array $args = []): string {
  114. $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
  115. if ($appName !== '') {
  116. $app_path = \OC_App::getAppPath($appName);
  117. // Check if the app is in the app folder
  118. if ($app_path && file_exists($app_path . '/' . $file)) {
  119. if (substr($file, -3) === 'php') {
  120. $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName;
  121. if ($frontControllerActive) {
  122. $urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName;
  123. }
  124. $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
  125. } else {
  126. $urlLinkTo = \OC_App::getAppWebPath($appName) . '/' . $file;
  127. }
  128. } else {
  129. $urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file;
  130. }
  131. } else {
  132. if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
  133. $urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
  134. } else {
  135. if ($frontControllerActive && $file === 'index.php') {
  136. $urlLinkTo = \OC::$WEBROOT . '/';
  137. } else {
  138. $urlLinkTo = \OC::$WEBROOT . '/' . $file;
  139. }
  140. }
  141. }
  142. if ($args && $query = http_build_query($args, '', '&')) {
  143. $urlLinkTo .= '?' . $query;
  144. }
  145. return $urlLinkTo;
  146. }
  147. /**
  148. * Creates path to an image
  149. *
  150. * @param string $appName app
  151. * @param string $file image name
  152. * @throws \RuntimeException If the image does not exist
  153. * @return string the url
  154. *
  155. * Returns the path to the image.
  156. */
  157. public function imagePath(string $appName, string $file): string {
  158. $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
  159. $cacheKey = $appName.'-'.$file;
  160. if ($key = $cache->get($cacheKey)) {
  161. return $key;
  162. }
  163. // Read the selected theme from the config file
  164. $theme = \OC_Util::getTheme();
  165. //if a theme has a png but not an svg always use the png
  166. $basename = substr(basename($file),0,-4);
  167. $appPath = \OC_App::getAppPath($appName);
  168. // Check if the app is in the app folder
  169. $path = '';
  170. $themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming');
  171. $themingImagePath = false;
  172. if ($themingEnabled) {
  173. $themingDefaults = \OC::$server->getThemingDefaults();
  174. if ($themingDefaults instanceof ThemingDefaults) {
  175. $themingImagePath = $themingDefaults->replaceImagePath($appName, $file);
  176. }
  177. }
  178. if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) {
  179. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file";
  180. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg")
  181. && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) {
  182. $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png";
  183. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) {
  184. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file";
  185. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg")
  186. && file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) {
  187. $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png";
  188. } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) {
  189. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$file";
  190. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
  191. && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
  192. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  193. } elseif ($themingEnabled && $themingImagePath) {
  194. $path = $themingImagePath;
  195. } elseif ($appPath && file_exists($appPath . "/img/$file")) {
  196. $path = \OC_App::getAppWebPath($appName) . "/img/$file";
  197. } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
  198. && file_exists($appPath . "/img/$basename.png")) {
  199. $path = \OC_App::getAppWebPath($appName) . "/img/$basename.png";
  200. } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) {
  201. $path = \OC::$WEBROOT . "/$appName/img/$file";
  202. } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg")
  203. && file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) {
  204. $path = \OC::$WEBROOT . "/$appName/img/$basename.png";
  205. } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) {
  206. $path = \OC::$WEBROOT . "/core/img/$file";
  207. } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg")
  208. && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) {
  209. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  210. }
  211. if ($path !== '') {
  212. $cache->set($cacheKey, $path);
  213. return $path;
  214. }
  215. throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
  216. }
  217. /**
  218. * Makes an URL absolute
  219. * @param string $url the url in the ownCloud host
  220. * @return string the absolute version of the url
  221. */
  222. public function getAbsoluteURL(string $url): string {
  223. $separator = strpos($url, '/') === 0 ? '' : '/';
  224. if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
  225. return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
  226. }
  227. // The ownCloud web root can already be prepended.
  228. if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) {
  229. $url = substr($url, \strlen(\OC::$WEBROOT));
  230. }
  231. return $this->getBaseUrl() . $separator . $url;
  232. }
  233. /**
  234. * @param string $key
  235. * @return string url to the online documentation
  236. */
  237. public function linkToDocs(string $key): string {
  238. $theme = \OC::$server->getThemingDefaults();
  239. return $theme->buildDocLinkToKey($key);
  240. }
  241. /**
  242. * @return string base url of the current request
  243. */
  244. public function getBaseUrl(): string {
  245. if ($this->baseUrl === null) {
  246. $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
  247. }
  248. return $this->baseUrl;
  249. }
  250. /**
  251. * @return string webroot part of the base url
  252. */
  253. public function getWebroot(): string {
  254. return \OC::$WEBROOT;
  255. }
  256. }