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.5KB

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