123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- declare(strict_types=1);
- 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\INavigationManager;
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use OCP\IUserSession;
- use RuntimeException;
- class URLGenerator implements IURLGenerator {
-
- private $config;
-
- public $userSession;
-
- private $cacheFactory;
-
- private $request;
-
- private $router;
-
- private $baseUrl = null;
- private ?IAppManager $appManager = null;
- private ?INavigationManager $navigationManager = 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;
- }
- private function getNavigationManager(): INavigationManager {
- if ($this->navigationManager !== null) {
- return $this->navigationManager;
- }
- $this->navigationManager = \OCP\Server::get(INavigationManager::class);
- return $this->navigationManager;
- }
-
- public function linkToRoute(string $routeName, array $arguments = []): string {
- return $this->router->generate($routeName, $arguments);
- }
-
- public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
- return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
- }
- public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string {
-
-
- $route = $this->router->generate('ocs.' . $routeName, $arguments, false);
-
- 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);
- }
-
- $route = substr($route, 7);
-
- $route = '/ocs/v2.php' . $route;
-
- return $this->getAbsoluteURL($route);
- }
-
- 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);
-
- if (file_exists($app_path . '/' . $file)) {
- if (str_ends_with($file, '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;
- }
-
- 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;
- }
-
- $theme = \OC_Util::getTheme();
-
- $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);
- }
- }
-
- $path = '';
- $themingEnabled = $this->config->getSystemValueBool('installed', false) && $this->getAppManager()->isEnabledForUser('theming');
- $themingImagePath = false;
- if ($themingEnabled) {
- $themingDefaults = \OC::$server->get('ThemingDefaults');
- 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);
- }
-
- public function getAbsoluteURL(string $url): string {
- $separator = str_starts_with($url, '/') ? '' : '/';
- if (\OC::$CLI && !\defined('PHPUNIT_RUN')) {
- return rtrim($this->config->getSystemValueString('overwrite.cli.url'), '/') . '/' . ltrim($url, '/');
- }
-
- if (\OC::$WEBROOT !== '' && str_starts_with($url, \OC::$WEBROOT)) {
- $url = substr($url, \strlen(\OC::$WEBROOT));
- }
- return $this->getBaseUrl() . $separator . $url;
- }
-
- public function linkToDocs(string $key): string {
- $theme = \OC::$server->get('ThemingDefaults');
- return $theme->buildDocLinkToKey($key);
- }
-
- public function linkToDefaultPageUrl(): string {
-
-
- if (isset($_REQUEST['redirect_url']) && !str_contains($_REQUEST['redirect_url'], '@')) {
- return $this->getAbsoluteURL(urldecode($_REQUEST['redirect_url']));
- }
- $defaultPage = $this->config->getAppValue('core', 'defaultpage');
- if ($defaultPage) {
- return $this->getAbsoluteURL($defaultPage);
- }
- $entryId = $this->getNavigationManager()->getDefaultEntryIdForUser();
- $entry = $this->getNavigationManager()->get($entryId);
- $href = (string)$entry['href'];
- if ($href === '') {
- throw new \InvalidArgumentException('Default navigation entry is missing href: ' . $entryId);
- }
- if (str_starts_with($href, '/index.php/') && ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) || getenv('front_controller_active') === 'true')) {
- $href = substr($href, 10);
- }
- return $this->getAbsoluteURL($href);
- }
-
- public function getBaseUrl(): string {
-
- if ($this->baseUrl === null || $this->baseUrl === 'http://' || $this->baseUrl === 'https://') {
- $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT;
- }
- return $this->baseUrl;
- }
-
- public function getWebroot(): string {
- return \OC::$WEBROOT;
- }
- }
|