urlgenerator.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @brief Creates an url using a defined route
  18. * @param $route
  19. * @param array $parameters
  20. * @return
  21. * @internal param array $args with param=>value, will be appended to the returned url
  22. * @returns string the url
  23. *
  24. * Returns a url to the given app and file.
  25. */
  26. public function linkToRoute($route, $parameters = array()) {
  27. $urlLinkTo = \OC::getRouter()->generate($route, $parameters);
  28. return $urlLinkTo;
  29. }
  30. /**
  31. * @brief Creates an url
  32. * @param string $app app
  33. * @param string $file file
  34. * @param array $args array with param=>value, will be appended to the returned url
  35. * The value of $args will be urlencoded
  36. * @return string the url
  37. *
  38. * Returns a url to the given app and file.
  39. */
  40. public function linkTo( $app, $file, $args = array() ) {
  41. if( $app != '' ) {
  42. $app_path = \OC_App::getAppPath($app);
  43. // Check if the app is in the app folder
  44. if ($app_path && file_exists($app_path . '/' . $file)) {
  45. if (substr($file, -3) == 'php' || substr($file, -3) == 'css') {
  46. $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app;
  47. $urlLinkTo .= ($file != 'index.php') ? '/' . $file : '';
  48. } else {
  49. $urlLinkTo = \OC_App::getAppWebPath($app) . '/' . $file;
  50. }
  51. } else {
  52. $urlLinkTo = \OC::$WEBROOT . '/' . $app . '/' . $file;
  53. }
  54. } else {
  55. if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
  56. $urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
  57. } else {
  58. $urlLinkTo = \OC::$WEBROOT . '/' . $file;
  59. }
  60. }
  61. if ($args && $query = http_build_query($args, '', '&')) {
  62. $urlLinkTo .= '?' . $query;
  63. }
  64. return $urlLinkTo;
  65. }
  66. /**
  67. * @brief Creates path to an image
  68. * @param string $app app
  69. * @param string $image image name
  70. * @return string the url
  71. *
  72. * Returns the path to the image.
  73. */
  74. public function imagePath($app, $image) {
  75. // Read the selected theme from the config file
  76. $theme = \OC_Util::getTheme();
  77. //if a theme has a png but not an svg always use the png
  78. $basename = substr(basename($image),0,-4);
  79. // Check if the app is in the app folder
  80. if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
  81. return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
  82. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg")
  83. && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.png")) {
  84. return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png";
  85. } elseif (file_exists(\OC_App::getAppPath($app) . "/img/$image")) {
  86. return \OC_App::getAppWebPath($app) . "/img/$image";
  87. } elseif (!file_exists(\OC_App::getAppPath($app) . "/img/$basename.svg")
  88. && file_exists(\OC_App::getAppPath($app) . "/img/$basename.png")) {
  89. return \OC_App::getAppPath($app) . "/img/$basename.png";
  90. } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) {
  91. return \OC::$WEBROOT . "/themes/$theme/$app/img/$image";
  92. } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.svg")
  93. && file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.png"))) {
  94. return \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png";
  95. } elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) {
  96. return \OC::$WEBROOT . "/$app/img/$image";
  97. } elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/$app/img/$basename.svg")
  98. && file_exists(\OC::$SERVERROOT . "/$app/img/$basename.png"))) {
  99. return \OC::$WEBROOT . "/$app/img/$basename.png";
  100. } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) {
  101. return \OC::$WEBROOT . "/themes/$theme/core/img/$image";
  102. } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
  103. && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
  104. return \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
  105. } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) {
  106. return \OC::$WEBROOT . "/core/img/$image";
  107. } else {
  108. throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
  109. }
  110. }
  111. /**
  112. * Makes an URL absolute
  113. * @param string $url the url in the owncloud host
  114. * @return string the absolute version of the url
  115. */
  116. public function getAbsoluteURL($url) {
  117. return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . $url;
  118. }
  119. }