urlgenerator.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. *
  8. */
  9. namespace OC;
  10. use OCP\IURLGenerator;
  11. use RuntimeException;
  12. /**
  13. * Class to generate URLs
  14. */
  15. class URLGenerator implements IURLGenerator {
  16. /**
  17. * @var \OCP\IConfig
  18. */
  19. private $config;
  20. /**
  21. * @param \OCP\IConfig $config
  22. */
  23. public function __construct($config) {
  24. $this->config = $config;
  25. }
  26. /**
  27. * Creates an url using a defined route
  28. * @param string $route
  29. * @param array $parameters
  30. * @internal param array $args with param=>value, will be appended to the returned url
  31. * @return string the url
  32. *
  33. * Returns a url to the given app and file.
  34. */
  35. public function linkToRoute($route, $parameters = array()) {
  36. $urlLinkTo = \OC::$server->getRouter()->generate($route, $parameters);
  37. return $urlLinkTo;
  38. }
  39. /**
  40. * Creates an url
  41. * @param string $app app
  42. * @param string $file file
  43. * @param array $args array with param=>value, will be appended to the returned url
  44. * The value of $args will be urlencoded
  45. * @return string the url
  46. *
  47. * Returns a url to the given app and file.
  48. */
  49. public function linkTo( $app, $file, $args = array() ) {
  50. $frontControllerActive=($this->config->getSystemValue('front_controller_active', 'false') == 'true');
  51. if( $app != '' ) {
  52. $app_path = \OC_App::getAppPath($app);
  53. // Check if the app is in the app folder
  54. if ($app_path && file_exists($app_path . '/' . $file)) {
  55. if (substr($file, -3) == 'php') {
  56. $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app;
  57. if ($frontControllerActive) {
  58. $urlLinkTo = \OC::$WEBROOT . '/apps/' . $app;
  59. }
  60. $urlLinkTo .= ($file != 'index.php') ? '/' . $file : '';
  61. } else {
  62. $urlLinkTo = \OC_App::getAppWebPath($app) . '/' . $file;
  63. }
  64. } else {
  65. $urlLinkTo = \OC::$WEBROOT . '/' . $app . '/' . $file;
  66. }
  67. } else {
  68. if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
  69. $urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
  70. } else {
  71. if ($frontControllerActive && $file === 'index.php') {
  72. $urlLinkTo = \OC::$WEBROOT;
  73. } else {
  74. $urlLinkTo = \OC::$WEBROOT . '/' . $file;
  75. }
  76. }
  77. }
  78. if ($args && $query = http_build_query($args, '', '&')) {
  79. $urlLinkTo .= '?' . $query;
  80. }
  81. return $urlLinkTo;
  82. }
  83. /**
  84. * Creates path to an image
  85. * @param string $app app
  86. * @param string $image image name
  87. * @throws \RuntimeException If the image does not exist
  88. * @return string the url
  89. *
  90. * Returns the path to the image.
  91. */
  92. public function imagePath($app, $image) {
  93. // Read the selected theme from the config file
  94. $theme = \OC_Util::getTheme();
  95. //if a theme has a png but not an svg always use the png
  96. $basename = substr(basename($image),0,-4);
  97. // Check if the app is in the app folder
  98. if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
  99. return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
  100. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg")
  101. && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.png")) {
  102. return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png";
  103. } elseif (file_exists(\OC_App::getAppPath($app) . "/img/$image")) {
  104. return \OC_App::getAppWebPath($app) . "/img/$image";
  105. } elseif (!file_exists(\OC_App::getAppPath($app) . "/img/$basename.svg")
  106. && file_exists(\OC_App::getAppPath($app) . "/img/$basename.png")) {
  107. return \OC_App::getAppPath($app) . "/img/$basename.png";
  108. } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) {
  109. return \OC::$WEBROOT . "/themes/$theme/$app/img/$image";
  110. } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.svg")
  111. && file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.png"))) {
  112. return \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png";
  113. } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) {
  114. return \OC::$WEBROOT . "/$app/img/$image";
  115. } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/$app/img/$basename.svg")
  116. && file_exists(\OC::$SERVERROOT . "/$app/img/$basename.png"))) {
  117. return \OC::$WEBROOT . "/$app/img/$basename.png";
  118. } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) {
  119. return \OC::$WEBROOT . "/themes/$theme/core/img/$image";
  120. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
  121. && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
  122. return \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  123. } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) {
  124. return \OC::$WEBROOT . "/core/img/$image";
  125. } else {
  126. throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
  127. }
  128. }
  129. /**
  130. * Makes an URL absolute
  131. * @param string $url the url in the owncloud host
  132. * @return string the absolute version of the url
  133. */
  134. public function getAbsoluteURL($url) {
  135. $separator = $url[0] === '/' ? '' : '/';
  136. // The ownCloud web root can already be prepended.
  137. $webRoot = substr($url, 0, strlen(\OC::$WEBROOT)) === \OC::$WEBROOT
  138. ? ''
  139. : \OC::$WEBROOT;
  140. return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost(). $webRoot . $separator . $url;
  141. }
  142. }