1
0

URLGenerator.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
  88. $route = \OC::$server->getRouter()->generate('ocs.'.$routeName, $arguments, false);
  89. if (strpos($route, '/index.php') === 0) {
  90. $route = substr($route, 10);
  91. }
  92. $route = substr($route, 7);
  93. $route = '/ocs/v2.php' . $route;
  94. return $this->getAbsoluteURL($route);
  95. }
  96. /**
  97. * Creates an url
  98. * @param string $app app
  99. * @param string $file file
  100. * @param array $args array with param=>value, will be appended to the returned url
  101. * The value of $args will be urlencoded
  102. * @return string the url
  103. *
  104. * Returns a url to the given app and file.
  105. */
  106. public function linkTo(string $app, string $file, array $args = array()): string {
  107. $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
  108. if( $app !== '' ) {
  109. $app_path = \OC_App::getAppPath($app);
  110. // Check if the app is in the app folder
  111. if ($app_path && file_exists($app_path . '/' . $file)) {
  112. if (substr($file, -3) === 'php') {
  113. $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app;
  114. if ($frontControllerActive) {
  115. $urlLinkTo = \OC::$WEBROOT . '/apps/' . $app;
  116. }
  117. $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
  118. } else {
  119. $urlLinkTo = \OC_App::getAppWebPath($app) . '/' . $file;
  120. }
  121. } else {
  122. $urlLinkTo = \OC::$WEBROOT . '/' . $app . '/' . $file;
  123. }
  124. } else {
  125. if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
  126. $urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
  127. } else {
  128. if ($frontControllerActive && $file === 'index.php') {
  129. $urlLinkTo = \OC::$WEBROOT . '/';
  130. } else {
  131. $urlLinkTo = \OC::$WEBROOT . '/' . $file;
  132. }
  133. }
  134. }
  135. if ($args && $query = http_build_query($args, '', '&')) {
  136. $urlLinkTo .= '?' . $query;
  137. }
  138. return $urlLinkTo;
  139. }
  140. /**
  141. * Creates path to an image
  142. * @param string $app app
  143. * @param string $image image name
  144. * @throws \RuntimeException If the image does not exist
  145. * @return string the url
  146. *
  147. * Returns the path to the image.
  148. */
  149. public function imagePath(string $app, string $image): string {
  150. $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
  151. $cacheKey = $app.'-'.$image;
  152. if($key = $cache->get($cacheKey)) {
  153. return $key;
  154. }
  155. // Read the selected theme from the config file
  156. $theme = \OC_Util::getTheme();
  157. //if a theme has a png but not an svg always use the png
  158. $basename = substr(basename($image),0,-4);
  159. $appPath = \OC_App::getAppPath($app);
  160. // Check if the app is in the app folder
  161. $path = '';
  162. $themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming');
  163. $themingImagePath = false;
  164. if($themingEnabled) {
  165. $themingDefaults = \OC::$server->getThemingDefaults();
  166. if ($themingDefaults instanceof ThemingDefaults) {
  167. $themingImagePath = $themingDefaults->replaceImagePath($app, $image);
  168. }
  169. }
  170. if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
  171. $path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
  172. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg")
  173. && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.png")) {
  174. $path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png";
  175. } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) {
  176. $path = \OC::$WEBROOT . "/themes/$theme/$app/img/$image";
  177. } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.svg")
  178. && file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.png"))) {
  179. $path = \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png";
  180. } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) {
  181. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$image";
  182. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
  183. && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
  184. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  185. } elseif($themingEnabled && $themingImagePath) {
  186. $path = $themingImagePath;
  187. } elseif ($appPath && file_exists($appPath . "/img/$image")) {
  188. $path = \OC_App::getAppWebPath($app) . "/img/$image";
  189. } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
  190. && file_exists($appPath . "/img/$basename.png")) {
  191. $path = \OC_App::getAppWebPath($app) . "/img/$basename.png";
  192. } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) {
  193. $path = \OC::$WEBROOT . "/$app/img/$image";
  194. } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/$app/img/$basename.svg")
  195. && file_exists(\OC::$SERVERROOT . "/$app/img/$basename.png"))) {
  196. $path = \OC::$WEBROOT . "/$app/img/$basename.png";
  197. } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) {
  198. $path = \OC::$WEBROOT . "/core/img/$image";
  199. } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg")
  200. && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) {
  201. $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  202. }
  203. if($path !== '') {
  204. $cache->set($cacheKey, $path);
  205. return $path;
  206. }
  207. throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
  208. }
  209. /**
  210. * Makes an URL absolute
  211. * @param string $url the url in the ownCloud host
  212. * @return string the absolute version of the url
  213. */
  214. public function getAbsoluteURL(string $url): string {
  215. $separator = strpos($url, '/') === 0 ? '' : '/';
  216. if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
  217. return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
  218. }
  219. // The ownCloud web root can already be prepended.
  220. if(\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) {
  221. $url = substr($url, \strlen(\OC::$WEBROOT));
  222. }
  223. return $this->getBaseUrl() . $separator . $url;
  224. }
  225. /**
  226. * @param string $key
  227. * @return string url to the online documentation
  228. */
  229. public function linkToDocs(string $key): string {
  230. $theme = \OC::$server->getThemingDefaults();
  231. return $theme->buildDocLinkToKey($key);
  232. }
  233. /**
  234. * @return string base url of the current request
  235. */
  236. public function getBaseUrl(): string {
  237. return $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
  238. }
  239. }