URLGenerator.php 8.9 KB

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