2013-09-26 20:41:19 +04:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-01-14 16:10:21 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Felix Epp <work@felixepp.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Julius Haertl <jus@bitgrid.net>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author mmccarn <mmccarn-github@mmsionline.us>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-09-26 20:41:19 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
namespace OC;
|
2016-07-22 17:49:33 +03:00
|
|
|
|
2018-02-27 12:15:31 +03:00
|
|
|
use OCA\Theming\ThemingDefaults;
|
2015-03-17 14:35:47 +03:00
|
|
|
use OCP\ICacheFactory;
|
|
|
|
use OCP\IConfig;
|
2017-06-15 22:51:51 +03:00
|
|
|
use OCP\IRequest;
|
2013-10-07 02:32:39 +04:00
|
|
|
use OCP\IURLGenerator;
|
2020-08-11 23:28:29 +03:00
|
|
|
use OCP\Route\IRouter;
|
2013-10-07 02:32:39 +04:00
|
|
|
use RuntimeException;
|
2013-09-26 20:41:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to generate URLs
|
|
|
|
*/
|
2013-10-07 02:32:39 +04:00
|
|
|
class URLGenerator implements IURLGenerator {
|
2015-03-17 14:35:47 +03:00
|
|
|
/** @var IConfig */
|
2013-12-31 17:36:02 +04:00
|
|
|
private $config;
|
2015-03-17 14:35:47 +03:00
|
|
|
/** @var ICacheFactory */
|
|
|
|
private $cacheFactory;
|
2017-06-15 22:51:51 +03:00
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
2020-08-11 23:28:29 +03:00
|
|
|
/** @var IRouter*/
|
|
|
|
private $router;
|
2021-03-10 19:22:31 +03:00
|
|
|
/** @var null|string */
|
|
|
|
private $baseUrl = null;
|
2013-12-31 17:36:02 +04:00
|
|
|
|
2015-03-17 14:35:47 +03:00
|
|
|
public function __construct(IConfig $config,
|
2017-06-15 22:51:51 +03:00
|
|
|
ICacheFactory $cacheFactory,
|
2020-08-11 23:28:29 +03:00
|
|
|
IRequest $request,
|
|
|
|
IRouter $router) {
|
2013-12-31 17:36:02 +04:00
|
|
|
$this->config = $config;
|
2015-03-17 14:35:47 +03:00
|
|
|
$this->cacheFactory = $cacheFactory;
|
2017-06-15 22:51:51 +03:00
|
|
|
$this->request = $request;
|
2020-08-11 23:28:29 +03:00
|
|
|
$this->router = $router;
|
2013-12-31 17:36:02 +04:00
|
|
|
}
|
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Creates an url using a defined route
|
2020-08-19 18:54:00 +03:00
|
|
|
*
|
|
|
|
* @param string $routeName
|
|
|
|
* @param array $arguments args with param=>value, will be appended to the returned url
|
2014-04-16 00:55:20 +04:00
|
|
|
* @return string the url
|
2013-09-26 20:41:19 +04:00
|
|
|
*
|
2014-09-08 16:43:14 +04:00
|
|
|
* Returns a url to the given route.
|
2013-09-26 20:41:19 +04:00
|
|
|
*/
|
2020-08-19 18:54:00 +03:00
|
|
|
public function linkToRoute(string $routeName, array $arguments = []): string {
|
2020-08-11 23:28:29 +03:00
|
|
|
return $this->router->generate($routeName, $arguments);
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
|
|
|
|
2014-09-08 16:43:14 +04:00
|
|
|
/**
|
|
|
|
* Creates an absolute url using a defined route
|
2015-02-12 18:35:47 +03:00
|
|
|
* @param string $routeName
|
|
|
|
* @param array $arguments args with param=>value, will be appended to the returned url
|
2014-09-08 16:43:14 +04:00
|
|
|
* @return string the url
|
|
|
|
*
|
|
|
|
* Returns an absolute url to the given route.
|
|
|
|
*/
|
2020-03-26 11:30:18 +03:00
|
|
|
public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
|
2014-09-08 16:43:14 +04:00
|
|
|
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
|
|
|
|
}
|
|
|
|
|
2018-10-04 22:06:00 +03:00
|
|
|
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
|
2020-08-11 23:28:29 +03:00
|
|
|
$route = $this->router->generate('ocs.'.$routeName, $arguments, false);
|
2018-10-04 22:06:00 +03:00
|
|
|
|
2019-11-29 13:14:21 +03:00
|
|
|
$indexPhpPos = strpos($route, '/index.php/');
|
|
|
|
if ($indexPhpPos !== false) {
|
|
|
|
$route = substr($route, $indexPhpPos + 10);
|
2018-10-04 22:06:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$route = substr($route, 7);
|
|
|
|
$route = '/ocs/v2.php' . $route;
|
|
|
|
|
|
|
|
return $this->getAbsoluteURL($route);
|
|
|
|
}
|
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Creates an url
|
2020-08-19 18:54:00 +03:00
|
|
|
*
|
|
|
|
* @param string $appName app
|
2013-09-26 20:41:19 +04:00
|
|
|
* @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.
|
|
|
|
*/
|
2020-08-19 18:54:00 +03:00
|
|
|
public function linkTo(string $appName, string $file, array $args = []): string {
|
2016-10-01 13:17:55 +03:00
|
|
|
$frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true');
|
2013-12-31 17:36:02 +04:00
|
|
|
|
2020-08-19 18:54:00 +03:00
|
|
|
if ($appName !== '') {
|
|
|
|
$app_path = \OC_App::getAppPath($appName);
|
2013-09-26 20:41:19 +04:00
|
|
|
// Check if the app is in the app folder
|
|
|
|
if ($app_path && file_exists($app_path . '/' . $file)) {
|
2018-01-14 16:10:21 +03:00
|
|
|
if (substr($file, -3) === 'php') {
|
2020-08-19 18:54:00 +03:00
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName;
|
2013-12-31 17:36:02 +04:00
|
|
|
if ($frontControllerActive) {
|
2020-08-19 18:54:00 +03:00
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName;
|
2013-12-31 17:36:02 +04:00
|
|
|
}
|
2018-01-14 16:10:21 +03:00
|
|
|
$urlLinkTo .= ($file !== 'index.php') ? '/' . $file : '';
|
2013-09-26 20:41:19 +04:00
|
|
|
} else {
|
2020-08-19 18:54:00 +03:00
|
|
|
$urlLinkTo = \OC_App::getAppWebPath($appName) . '/' . $file;
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-19 18:54:00 +03:00
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file;
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) {
|
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/core/' . $file;
|
|
|
|
} else {
|
2013-12-31 17:36:02 +04:00
|
|
|
if ($frontControllerActive && $file === 'index.php') {
|
2015-12-02 13:27:07 +03:00
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/';
|
2013-12-31 17:36:02 +04:00
|
|
|
} else {
|
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/' . $file;
|
|
|
|
}
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($args && $query = http_build_query($args, '', '&')) {
|
|
|
|
$urlLinkTo .= '?' . $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $urlLinkTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Creates path to an image
|
2020-08-19 18:54:00 +03:00
|
|
|
*
|
|
|
|
* @param string $appName app
|
|
|
|
* @param string $file image name
|
2014-04-21 17:44:54 +04:00
|
|
|
* @throws \RuntimeException If the image does not exist
|
2013-09-26 20:41:19 +04:00
|
|
|
* @return string the url
|
|
|
|
*
|
|
|
|
* Returns the path to the image.
|
|
|
|
*/
|
2020-08-19 18:54:00 +03:00
|
|
|
public function imagePath(string $appName, string $file): string {
|
2017-12-15 13:31:13 +03:00
|
|
|
$cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-');
|
2020-08-19 18:54:00 +03:00
|
|
|
$cacheKey = $appName.'-'.$file;
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($key = $cache->get($cacheKey)) {
|
2015-03-17 15:15:52 +03:00
|
|
|
return $key;
|
2015-03-17 14:35:47 +03:00
|
|
|
}
|
|
|
|
|
2015-03-17 14:47:46 +03:00
|
|
|
// Read the selected theme from the config file
|
|
|
|
$theme = \OC_Util::getTheme();
|
|
|
|
|
2013-10-23 18:44:56 +04:00
|
|
|
//if a theme has a png but not an svg always use the png
|
2020-08-19 18:54:00 +03:00
|
|
|
$basename = substr(basename($file),0,-4);
|
2013-10-23 18:44:56 +04:00
|
|
|
|
2020-08-19 18:54:00 +03:00
|
|
|
$appPath = \OC_App::getAppPath($appName);
|
2015-04-04 01:42:18 +03:00
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
// Check if the app is in the app folder
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = '';
|
2016-12-09 14:52:17 +03:00
|
|
|
$themingEnabled = $this->config->getSystemValue('installed', false) && \OCP\App::isEnabled('theming') && \OC_App::isAppLoaded('theming');
|
2017-09-11 16:03:47 +03:00
|
|
|
$themingImagePath = false;
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($themingEnabled) {
|
2018-02-27 12:15:31 +03:00
|
|
|
$themingDefaults = \OC::$server->getThemingDefaults();
|
|
|
|
if ($themingDefaults instanceof ThemingDefaults) {
|
2020-08-19 18:54:00 +03:00
|
|
|
$themingImagePath = $themingDefaults->replaceImagePath($appName, $file);
|
2018-02-27 12:15:31 +03:00
|
|
|
}
|
2017-09-11 16:03:47 +03:00
|
|
|
}
|
|
|
|
|
2020-08-19 18:54:00 +03:00
|
|
|
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";
|
2013-10-23 18:44:56 +04:00
|
|
|
} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg")
|
2013-10-28 17:18:20 +04:00
|
|
|
&& file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
|
2020-04-10 15:19:56 +03:00
|
|
|
} elseif ($themingEnabled && $themingImagePath) {
|
2017-09-11 16:03:47 +03:00
|
|
|
$path = $themingImagePath;
|
2020-08-19 18:54:00 +03:00
|
|
|
} elseif ($appPath && file_exists($appPath . "/img/$file")) {
|
|
|
|
$path = \OC_App::getAppWebPath($appName) . "/img/$file";
|
2016-04-04 13:57:52 +03:00
|
|
|
} elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
|
|
|
|
&& file_exists($appPath . "/img/$basename.png")) {
|
2020-08-19 18:54:00 +03:00
|
|
|
$path = \OC_App::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";
|
2016-04-04 13:57:52 +03:00
|
|
|
} 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";
|
2015-03-17 14:35:47 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($path !== '') {
|
2015-03-17 14:35:47 +03:00
|
|
|
$cache->set($cacheKey, $path);
|
|
|
|
return $path;
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
2018-01-14 16:10:21 +03:00
|
|
|
|
2020-08-19 18:54:00 +03:00
|
|
|
throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
|
|
|
|
2013-10-07 02:32:39 +04:00
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
/**
|
2013-10-07 02:32:39 +04:00
|
|
|
* Makes an URL absolute
|
2014-11-24 18:24:26 +03:00
|
|
|
* @param string $url the url in the ownCloud host
|
2013-10-07 02:32:39 +04:00
|
|
|
* @return string the absolute version of the url
|
2013-09-26 20:41:19 +04:00
|
|
|
*/
|
2018-01-14 16:10:21 +03:00
|
|
|
public function getAbsoluteURL(string $url): string {
|
2018-11-20 15:28:07 +03:00
|
|
|
$separator = strpos($url, '/') === 0 ? '' : '/';
|
2014-03-07 02:56:11 +04:00
|
|
|
|
2018-01-14 16:10:21 +03:00
|
|
|
if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
|
2014-10-13 13:18:24 +04:00
|
|
|
return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
|
|
|
|
}
|
2014-03-31 16:24:38 +04:00
|
|
|
// The ownCloud web root can already be prepended.
|
2020-04-10 15:19:56 +03:00
|
|
|
if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) {
|
2018-01-14 16:10:21 +03:00
|
|
|
$url = substr($url, \strlen(\OC::$WEBROOT));
|
2017-06-15 13:35:16 +03:00
|
|
|
}
|
2014-03-31 16:24:38 +04:00
|
|
|
|
2017-06-15 13:35:16 +03:00
|
|
|
return $this->getBaseUrl() . $separator . $url;
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
2014-11-24 18:24:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $key
|
|
|
|
* @return string url to the online documentation
|
|
|
|
*/
|
2018-01-14 16:10:21 +03:00
|
|
|
public function linkToDocs(string $key): string {
|
2016-07-15 09:46:31 +03:00
|
|
|
$theme = \OC::$server->getThemingDefaults();
|
2014-11-24 18:24:26 +03:00
|
|
|
return $theme->buildDocLinkToKey($key);
|
|
|
|
}
|
2017-06-15 13:35:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string base url of the current request
|
|
|
|
*/
|
2018-01-14 16:10:21 +03:00
|
|
|
public function getBaseUrl(): string {
|
2021-03-10 19:22:31 +03:00
|
|
|
if ($this->baseUrl === null) {
|
|
|
|
$this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
|
|
|
|
}
|
|
|
|
return $this->baseUrl;
|
2017-06-15 13:35:16 +03:00
|
|
|
}
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|