urlgenerator.php 7.5 KB

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