urlgenerator.php 7.5 KB

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