1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
* @author Bart Visscher <bartv@thisnet.nl>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Daniel Rudolf <github.com@daniel-rudolf.de>
* @author Felix Epp <work@felixepp.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
* @author Julius Haertl <jus@bitgrid.net>
* @author Julius Härtl <jus@bitgrid.net>
* @author Lukas Reschke <lukas@statuscode.ch>
* @author mmccarn <mmccarn-github@mmsionline.us>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
* @author Robin McCorkell <robin@mccorkell.me.uk>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC;
use OC\Route\Router;
use OCA\Theming\ThemingDefaults;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use RuntimeException;
/**
* Class to generate URLs
*/
class URLGenerator implements IURLGenerator {
/** @var IConfig */
private $config;
/** @var IUserSession */
public $userSession;
/** @var ICacheFactory */
private $cacheFactory;
/** @var IRequest */
private $request;
/** @var Router */
private $router;
/** @var null|string */
private $baseUrl = null;
private ?IAppManager $appManager = null;
public function __construct(IConfig $config,
IUserSession $userSession,
ICacheFactory $cacheFactory,
IRequest $request,
Router $router
) {
$this->config = $config;
$this->userSession = $userSession;
$this->cacheFactory = $cacheFactory;
$this->request = $request;
$this->router = $router;
}
private function getAppManager(): IAppManager {
if ($this->appManager !== null) {
return $this->appManager;
}
$this->appManager = \OCP\Server::get(IAppManager::class);
return $this->appManager;
}
/**
* Creates an url using a defined route
*
* @param string $routeName
* @param array $arguments args with param=>value, will be appended to the returned url
* @return string the url
*
* Returns a url to the given route.
*/
public function linkToRoute(string $routeName, array $arguments = []): string {
return $this->router->generate($routeName, $arguments);
}
/**
* Creates an absolute url using a defined route
* @param string $routeName
* @param array $arguments args with param=>value, will be appended to the returned url
* @return string the url
*
* Returns an absolute url to the given route.
*/
public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
}
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
// Returns `/subfolder/index.php/ocsapp/…` with `'htaccess.IgnoreFrontController' => false` in config.php
// And `/subfolder/ocsapp/…` with `'htaccess.IgnoreFrontController' => true` in config.php
$route = $this->router->generate('ocs.'.$routeName, $arguments, false);
// Cut off `/subfolder`
if (\OC::$WEBROOT !== '' && str_starts_with($route, \OC::$WEBROOT)) {
$route = substr($route, \strlen(\OC::$WEBROOT));
}
if (str_starts_with($route, '/index.php/')) {
$route = substr($route, 10);
}
// Remove `ocsapp/` bit
$route = substr($route, 7);
// Prefix with ocs/v2.php endpoint
$route = '/ocs/v2.php' . $route;
// Turn into an absolute URL
return $this->getAbsoluteURL($route);
}
/**
* Creates an url
*
* @param string $appName 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(string $appName, string $file, array $args = []): string {
$frontControllerActive = ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) || getenv('front_controller_active') === 'true');
if ($appName !== '') {
$app_path = $this->getAppManager()->getAppPath($appName);
// Check if the app is in the app folder
if (file_exists($app_path . '/' . $file)) {
if (substr($file, -3) === 'php') {
$urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName;
if ($frontControllerActive) {
$urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName;
}
$urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
} else {
$urlLinkTo = $this->getAppManager()->getAppWebPath($appName) . '/' . $file;
}
} else {
$urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file;
}
} else {
if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
$urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
} else {
if ($frontControllerActive && $file === 'index.php') {
$urlLinkTo = \OC::$WEBROOT . '/';
} else {
$urlLinkTo = \OC::$WEBROOT . '/' . $file;
}
}
}
if ($args && $query = http_build_query($args, '', '&')) {
$urlLinkTo .= '?' . $query;
}
return $urlLinkTo;
}
/**
* Creates path to an image
*
* @param string $appName app
* @param string $file image name
* @throws \RuntimeException If the image does not exist
* @return string the url
*
* Returns the path to the image.
*/
public function imagePath(string $appName, string $file): string {
$cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
$cacheKey = $appName.'-'.$file;
if ($key = $cache->get($cacheKey)) {
return $key;
}
// 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($file), 0, -4);
try {
$appPath = $this->getAppManager()->getAppPath($appName);
} catch (AppPathNotFoundException $e) {
if ($appName === 'core' || $appName === '') {
$appName = 'core';
$appPath = false;
} else {
throw new RuntimeException('image not found: image: ' . $file . ' webroot: ' . \OC::$WEBROOT . ' serverroot: ' . \OC::$SERVERROOT);
}
}
// Check if the app is in the app folder
$path = '';
$themingEnabled = $this->config->getSystemValueBool('installed', false) && $this->getAppManager()->isEnabledForUser('theming');
$themingImagePath = false;
if ($themingEnabled) {
$themingDefaults = \OC::$server->getThemingDefaults();
if ($themingDefaults instanceof ThemingDefaults) {
$themingImagePath = $themingDefaults->replaceImagePath($appName, $file);
}
}
if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) {
$path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file";
} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg")
&& file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) {
$path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png";
} elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) {
$path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file";
} elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg")
&& file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) {
$path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png";
} elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) {
$path = \OC::$WEBROOT . "/themes/$theme/core/img/$file";
} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
&& file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
$path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
} elseif ($themingEnabled && $themingImagePath) {
$path = $themingImagePath;
} elseif ($appPath && file_exists($appPath . "/img/$file")) {
$path = $this->getAppManager()->getAppWebPath($appName) . "/img/$file";
} elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
&& file_exists($appPath . "/img/$basename.png")) {
$path = $this->getAppManager()->getAppWebPath($appName) . "/img/$basename.png";
} elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) {
$path = \OC::$WEBROOT . "/$appName/img/$file";
} elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg")
&& file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) {
$path = \OC::$WEBROOT . "/$appName/img/$basename.png";
} elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) {
$path = \OC::$WEBROOT . "/core/img/$file";
} elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg")
&& file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) {
$path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
}
if ($path !== '') {
$cache->set($cacheKey, $path);
return $path;
}
throw new RuntimeException('image not found: image:' . $file . ' 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(string $url): string {
$separator = strpos($url, '/') === 0 ? '' : '/';
if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
return rtrim($this->config->getSystemValueString('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
}
// The ownCloud web root can already be prepended.
if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) {
$url = substr($url, \strlen(\OC::$WEBROOT));
}
return $this->getBaseUrl() . $separator . $url;
}
/**
* @param string $key
* @return string url to the online documentation
*/
public function linkToDocs(string $key): string {
$theme = \OC::$server->getThemingDefaults();
return $theme->buildDocLinkToKey($key);
}
/**
* Returns the URL of the default page based on the system configuration
* and the apps visible for the current user
* @return string
*/
public function linkToDefaultPageUrl(): string {
// Deny the redirect if the URL contains a @
// This prevents unvalidated redirects like ?redirect_url=:user@domain.com
if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) {
return $this->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
}
$defaultPage = $this->config->getAppValue('core', 'defaultpage');
if ($defaultPage) {
return $this->getAbsoluteURL($defaultPage);
}
$appId = $this->getAppManager()->getDefaultAppForUser();
if ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false)
|| getenv('front_controller_active') === 'true') {
return $this->getAbsoluteURL('/apps/' . $appId . '/');
}
return $this->getAbsoluteURL('/index.php/apps/' . $appId . '/');
}
/**
* @return string base url of the current request
*/
public function getBaseUrl(): string {
// BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup.
if ($this->baseUrl === null || $this->baseUrl === "http://" || $this->baseUrl === "https://") {
$this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
}
return $this->baseUrl;
}
/**
* @return string webroot part of the base url
*/
public function getWebroot(): string {
return \OC::$WEBROOT;
}
}
|