URLGenerator.php 8.7 KB

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