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