URLGenerator.php 7.7 KB

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