@@ -55,303 +55,303 @@ |
||
| 55 | 55 | * Class to generate URLs |
| 56 | 56 | */ |
| 57 | 57 | class URLGenerator implements IURLGenerator { |
| 58 | - /** @var IConfig */ |
|
| 59 | - private $config; |
|
| 60 | - /** @var IUserSession */ |
|
| 61 | - public $userSession; |
|
| 62 | - /** @var ICacheFactory */ |
|
| 63 | - private $cacheFactory; |
|
| 64 | - /** @var IRequest */ |
|
| 65 | - private $request; |
|
| 66 | - /** @var Router */ |
|
| 67 | - private $router; |
|
| 68 | - /** @var null|string */ |
|
| 69 | - private $baseUrl = null; |
|
| 70 | - private ?IAppManager $appManager = null; |
|
| 71 | - |
|
| 72 | - public function __construct(IConfig $config, |
|
| 73 | - IUserSession $userSession, |
|
| 74 | - ICacheFactory $cacheFactory, |
|
| 75 | - IRequest $request, |
|
| 76 | - Router $router |
|
| 77 | - ) { |
|
| 78 | - $this->config = $config; |
|
| 79 | - $this->userSession = $userSession; |
|
| 80 | - $this->cacheFactory = $cacheFactory; |
|
| 81 | - $this->request = $request; |
|
| 82 | - $this->router = $router; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - private function getAppManager(): IAppManager { |
|
| 86 | - if ($this->appManager !== null) { |
|
| 87 | - return $this->appManager; |
|
| 88 | - } |
|
| 89 | - $this->appManager = \OCP\Server::get(IAppManager::class); |
|
| 90 | - return $this->appManager; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Creates an url using a defined route |
|
| 95 | - * |
|
| 96 | - * @param string $routeName |
|
| 97 | - * @param array $arguments args with param=>value, will be appended to the returned url |
|
| 98 | - * @return string the url |
|
| 99 | - * |
|
| 100 | - * Returns a url to the given route. |
|
| 101 | - */ |
|
| 102 | - public function linkToRoute(string $routeName, array $arguments = []): string { |
|
| 103 | - return $this->router->generate($routeName, $arguments); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Creates an absolute url using a defined route |
|
| 108 | - * @param string $routeName |
|
| 109 | - * @param array $arguments args with param=>value, will be appended to the returned url |
|
| 110 | - * @return string the url |
|
| 111 | - * |
|
| 112 | - * Returns an absolute url to the given route. |
|
| 113 | - */ |
|
| 114 | - public function linkToRouteAbsolute(string $routeName, array $arguments = []): string { |
|
| 115 | - return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments)); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string { |
|
| 119 | - $route = $this->router->generate('ocs.'.$routeName, $arguments, false); |
|
| 120 | - |
|
| 121 | - $indexPhpPos = strpos($route, '/index.php/'); |
|
| 122 | - if ($indexPhpPos !== false) { |
|
| 123 | - $route = substr($route, $indexPhpPos + 10); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $route = substr($route, 7); |
|
| 127 | - $route = '/ocs/v2.php' . $route; |
|
| 128 | - |
|
| 129 | - return $this->getAbsoluteURL($route); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Creates an url |
|
| 134 | - * |
|
| 135 | - * @param string $appName app |
|
| 136 | - * @param string $file file |
|
| 137 | - * @param array $args array with param=>value, will be appended to the returned url |
|
| 138 | - * The value of $args will be urlencoded |
|
| 139 | - * @return string the url |
|
| 140 | - * |
|
| 141 | - * Returns a url to the given app and file. |
|
| 142 | - */ |
|
| 143 | - public function linkTo(string $appName, string $file, array $args = []): string { |
|
| 144 | - $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 145 | - |
|
| 146 | - if ($appName !== '') { |
|
| 147 | - $app_path = $this->getAppManager()->getAppPath($appName); |
|
| 148 | - // Check if the app is in the app folder |
|
| 149 | - if (file_exists($app_path . '/' . $file)) { |
|
| 150 | - if (substr($file, -3) === 'php') { |
|
| 151 | - $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName; |
|
| 152 | - if ($frontControllerActive) { |
|
| 153 | - $urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName; |
|
| 154 | - } |
|
| 155 | - $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : ''; |
|
| 156 | - } else { |
|
| 157 | - $urlLinkTo = $this->getAppManager()->getAppWebPath($appName) . '/' . $file; |
|
| 158 | - } |
|
| 159 | - } else { |
|
| 160 | - $urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file; |
|
| 161 | - } |
|
| 162 | - } else { |
|
| 163 | - if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) { |
|
| 164 | - $urlLinkTo = \OC::$WEBROOT . '/core/' . $file; |
|
| 165 | - } else { |
|
| 166 | - if ($frontControllerActive && $file === 'index.php') { |
|
| 167 | - $urlLinkTo = \OC::$WEBROOT . '/'; |
|
| 168 | - } else { |
|
| 169 | - $urlLinkTo = \OC::$WEBROOT . '/' . $file; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if ($args && $query = http_build_query($args, '', '&')) { |
|
| 175 | - $urlLinkTo .= '?' . $query; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - return $urlLinkTo; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Creates path to an image |
|
| 183 | - * |
|
| 184 | - * @param string $appName app |
|
| 185 | - * @param string $file image name |
|
| 186 | - * @throws \RuntimeException If the image does not exist |
|
| 187 | - * @return string the url |
|
| 188 | - * |
|
| 189 | - * Returns the path to the image. |
|
| 190 | - */ |
|
| 191 | - public function imagePath(string $appName, string $file): string { |
|
| 192 | - $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-'); |
|
| 193 | - $cacheKey = $appName.'-'.$file; |
|
| 194 | - if ($key = $cache->get($cacheKey)) { |
|
| 195 | - return $key; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - // Read the selected theme from the config file |
|
| 199 | - $theme = \OC_Util::getTheme(); |
|
| 200 | - |
|
| 201 | - //if a theme has a png but not an svg always use the png |
|
| 202 | - $basename = substr(basename($file), 0, -4); |
|
| 203 | - |
|
| 204 | - try { |
|
| 205 | - $appPath = $this->getAppManager()->getAppPath($appName); |
|
| 206 | - } catch (AppPathNotFoundException $e) { |
|
| 207 | - if ($appName === 'core' || $appName === '') { |
|
| 208 | - $appName = 'core'; |
|
| 209 | - $appPath = false; |
|
| 210 | - } else { |
|
| 211 | - throw new RuntimeException('image not found: image: ' . $file . ' webroot: ' . \OC::$WEBROOT . ' serverroot: ' . \OC::$SERVERROOT); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - // Check if the app is in the app folder |
|
| 216 | - $path = ''; |
|
| 217 | - $themingEnabled = $this->config->getSystemValue('installed', false) && $this->getAppManager()->isEnabledForUser('theming'); |
|
| 218 | - $themingImagePath = false; |
|
| 219 | - if ($themingEnabled) { |
|
| 220 | - $themingDefaults = \OC::$server->getThemingDefaults(); |
|
| 221 | - if ($themingDefaults instanceof ThemingDefaults) { |
|
| 222 | - $themingImagePath = $themingDefaults->replaceImagePath($appName, $file); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) { |
|
| 227 | - $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file"; |
|
| 228 | - } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg") |
|
| 229 | - && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) { |
|
| 230 | - $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png"; |
|
| 231 | - } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) { |
|
| 232 | - $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file"; |
|
| 233 | - } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg") |
|
| 234 | - && file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) { |
|
| 235 | - $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png"; |
|
| 236 | - } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) { |
|
| 237 | - $path = \OC::$WEBROOT . "/themes/$theme/core/img/$file"; |
|
| 238 | - } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg") |
|
| 239 | - && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) { |
|
| 240 | - $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
|
| 241 | - } elseif ($themingEnabled && $themingImagePath) { |
|
| 242 | - $path = $themingImagePath; |
|
| 243 | - } elseif ($appPath && file_exists($appPath . "/img/$file")) { |
|
| 244 | - $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$file"; |
|
| 245 | - } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg") |
|
| 246 | - && file_exists($appPath . "/img/$basename.png")) { |
|
| 247 | - $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$basename.png"; |
|
| 248 | - } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) { |
|
| 249 | - $path = \OC::$WEBROOT . "/$appName/img/$file"; |
|
| 250 | - } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg") |
|
| 251 | - && file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) { |
|
| 252 | - $path = \OC::$WEBROOT . "/$appName/img/$basename.png"; |
|
| 253 | - } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) { |
|
| 254 | - $path = \OC::$WEBROOT . "/core/img/$file"; |
|
| 255 | - } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg") |
|
| 256 | - && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) { |
|
| 257 | - $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - if ($path !== '') { |
|
| 261 | - $cache->set($cacheKey, $path); |
|
| 262 | - return $path; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * Makes an URL absolute |
|
| 271 | - * @param string $url the url in the ownCloud host |
|
| 272 | - * @return string the absolute version of the url |
|
| 273 | - */ |
|
| 274 | - public function getAbsoluteURL(string $url): string { |
|
| 275 | - $separator = strpos($url, '/') === 0 ? '' : '/'; |
|
| 276 | - |
|
| 277 | - if (\OC::$CLI && !\defined('PHPUNIT_RUN')) { |
|
| 278 | - return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/'); |
|
| 279 | - } |
|
| 280 | - // The ownCloud web root can already be prepended. |
|
| 281 | - if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) { |
|
| 282 | - $url = substr($url, \strlen(\OC::$WEBROOT)); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - return $this->getBaseUrl() . $separator . $url; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * @param string $key |
|
| 290 | - * @return string url to the online documentation |
|
| 291 | - */ |
|
| 292 | - public function linkToDocs(string $key): string { |
|
| 293 | - $theme = \OC::$server->getThemingDefaults(); |
|
| 294 | - return $theme->buildDocLinkToKey($key); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Returns the URL of the default page based on the system configuration |
|
| 299 | - * and the apps visible for the current user |
|
| 300 | - * @return string |
|
| 301 | - */ |
|
| 302 | - public function linkToDefaultPageUrl(): string { |
|
| 303 | - // Deny the redirect if the URL contains a @ |
|
| 304 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 305 | - if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) { |
|
| 306 | - return $this->getAbsoluteURL(urldecode($_REQUEST['redirect_url'])); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - $defaultPage = $this->config->getAppValue('core', 'defaultpage'); |
|
| 310 | - if ($defaultPage) { |
|
| 311 | - return $this->getAbsoluteURL($defaultPage); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $appId = 'files'; |
|
| 315 | - $defaultApps = explode(',', $this->config->getSystemValue('defaultapp', 'dashboard,files')); |
|
| 316 | - |
|
| 317 | - $userId = $this->userSession->isLoggedIn() ? $this->userSession->getUser()->getUID() : null; |
|
| 318 | - if ($userId !== null) { |
|
| 319 | - $userDefaultApps = explode(',', $this->config->getUserValue($userId, 'core', 'defaultapp')); |
|
| 320 | - $defaultApps = array_filter(array_merge($userDefaultApps, $defaultApps)); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - // find the first app that is enabled for the current user |
|
| 324 | - foreach ($defaultApps as $defaultApp) { |
|
| 325 | - $defaultApp = \OC_App::cleanAppId(strip_tags($defaultApp)); |
|
| 326 | - if (\OC::$server->getAppManager()->isEnabledForUser($defaultApp)) { |
|
| 327 | - $appId = $defaultApp; |
|
| 328 | - break; |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - if ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true |
|
| 333 | - || getenv('front_controller_active') === 'true') { |
|
| 334 | - return $this->getAbsoluteURL('/apps/' . $appId . '/'); |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - return $this->getAbsoluteURL('/index.php/apps/' . $appId . '/'); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @return string base url of the current request |
|
| 342 | - */ |
|
| 343 | - public function getBaseUrl(): string { |
|
| 344 | - // BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup. |
|
| 345 | - if ($this->baseUrl === null || $this->baseUrl === "http://" || $this->baseUrl === "https://") { |
|
| 346 | - $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT; |
|
| 347 | - } |
|
| 348 | - return $this->baseUrl; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * @return string webroot part of the base url |
|
| 353 | - */ |
|
| 354 | - public function getWebroot(): string { |
|
| 355 | - return \OC::$WEBROOT; |
|
| 356 | - } |
|
| 58 | + /** @var IConfig */ |
|
| 59 | + private $config; |
|
| 60 | + /** @var IUserSession */ |
|
| 61 | + public $userSession; |
|
| 62 | + /** @var ICacheFactory */ |
|
| 63 | + private $cacheFactory; |
|
| 64 | + /** @var IRequest */ |
|
| 65 | + private $request; |
|
| 66 | + /** @var Router */ |
|
| 67 | + private $router; |
|
| 68 | + /** @var null|string */ |
|
| 69 | + private $baseUrl = null; |
|
| 70 | + private ?IAppManager $appManager = null; |
|
| 71 | + |
|
| 72 | + public function __construct(IConfig $config, |
|
| 73 | + IUserSession $userSession, |
|
| 74 | + ICacheFactory $cacheFactory, |
|
| 75 | + IRequest $request, |
|
| 76 | + Router $router |
|
| 77 | + ) { |
|
| 78 | + $this->config = $config; |
|
| 79 | + $this->userSession = $userSession; |
|
| 80 | + $this->cacheFactory = $cacheFactory; |
|
| 81 | + $this->request = $request; |
|
| 82 | + $this->router = $router; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + private function getAppManager(): IAppManager { |
|
| 86 | + if ($this->appManager !== null) { |
|
| 87 | + return $this->appManager; |
|
| 88 | + } |
|
| 89 | + $this->appManager = \OCP\Server::get(IAppManager::class); |
|
| 90 | + return $this->appManager; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Creates an url using a defined route |
|
| 95 | + * |
|
| 96 | + * @param string $routeName |
|
| 97 | + * @param array $arguments args with param=>value, will be appended to the returned url |
|
| 98 | + * @return string the url |
|
| 99 | + * |
|
| 100 | + * Returns a url to the given route. |
|
| 101 | + */ |
|
| 102 | + public function linkToRoute(string $routeName, array $arguments = []): string { |
|
| 103 | + return $this->router->generate($routeName, $arguments); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Creates an absolute url using a defined route |
|
| 108 | + * @param string $routeName |
|
| 109 | + * @param array $arguments args with param=>value, will be appended to the returned url |
|
| 110 | + * @return string the url |
|
| 111 | + * |
|
| 112 | + * Returns an absolute url to the given route. |
|
| 113 | + */ |
|
| 114 | + public function linkToRouteAbsolute(string $routeName, array $arguments = []): string { |
|
| 115 | + return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments)); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string { |
|
| 119 | + $route = $this->router->generate('ocs.'.$routeName, $arguments, false); |
|
| 120 | + |
|
| 121 | + $indexPhpPos = strpos($route, '/index.php/'); |
|
| 122 | + if ($indexPhpPos !== false) { |
|
| 123 | + $route = substr($route, $indexPhpPos + 10); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $route = substr($route, 7); |
|
| 127 | + $route = '/ocs/v2.php' . $route; |
|
| 128 | + |
|
| 129 | + return $this->getAbsoluteURL($route); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Creates an url |
|
| 134 | + * |
|
| 135 | + * @param string $appName app |
|
| 136 | + * @param string $file file |
|
| 137 | + * @param array $args array with param=>value, will be appended to the returned url |
|
| 138 | + * The value of $args will be urlencoded |
|
| 139 | + * @return string the url |
|
| 140 | + * |
|
| 141 | + * Returns a url to the given app and file. |
|
| 142 | + */ |
|
| 143 | + public function linkTo(string $appName, string $file, array $args = []): string { |
|
| 144 | + $frontControllerActive = ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true || getenv('front_controller_active') === 'true'); |
|
| 145 | + |
|
| 146 | + if ($appName !== '') { |
|
| 147 | + $app_path = $this->getAppManager()->getAppPath($appName); |
|
| 148 | + // Check if the app is in the app folder |
|
| 149 | + if (file_exists($app_path . '/' . $file)) { |
|
| 150 | + if (substr($file, -3) === 'php') { |
|
| 151 | + $urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $appName; |
|
| 152 | + if ($frontControllerActive) { |
|
| 153 | + $urlLinkTo = \OC::$WEBROOT . '/apps/' . $appName; |
|
| 154 | + } |
|
| 155 | + $urlLinkTo .= ($file !== 'index.php') ? '/' . $file : ''; |
|
| 156 | + } else { |
|
| 157 | + $urlLinkTo = $this->getAppManager()->getAppWebPath($appName) . '/' . $file; |
|
| 158 | + } |
|
| 159 | + } else { |
|
| 160 | + $urlLinkTo = \OC::$WEBROOT . '/' . $appName . '/' . $file; |
|
| 161 | + } |
|
| 162 | + } else { |
|
| 163 | + if (file_exists(\OC::$SERVERROOT . '/core/' . $file)) { |
|
| 164 | + $urlLinkTo = \OC::$WEBROOT . '/core/' . $file; |
|
| 165 | + } else { |
|
| 166 | + if ($frontControllerActive && $file === 'index.php') { |
|
| 167 | + $urlLinkTo = \OC::$WEBROOT . '/'; |
|
| 168 | + } else { |
|
| 169 | + $urlLinkTo = \OC::$WEBROOT . '/' . $file; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if ($args && $query = http_build_query($args, '', '&')) { |
|
| 175 | + $urlLinkTo .= '?' . $query; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + return $urlLinkTo; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Creates path to an image |
|
| 183 | + * |
|
| 184 | + * @param string $appName app |
|
| 185 | + * @param string $file image name |
|
| 186 | + * @throws \RuntimeException If the image does not exist |
|
| 187 | + * @return string the url |
|
| 188 | + * |
|
| 189 | + * Returns the path to the image. |
|
| 190 | + */ |
|
| 191 | + public function imagePath(string $appName, string $file): string { |
|
| 192 | + $cache = $this->cacheFactory->createDistributed('imagePath-'.md5($this->getBaseUrl()).'-'); |
|
| 193 | + $cacheKey = $appName.'-'.$file; |
|
| 194 | + if ($key = $cache->get($cacheKey)) { |
|
| 195 | + return $key; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + // Read the selected theme from the config file |
|
| 199 | + $theme = \OC_Util::getTheme(); |
|
| 200 | + |
|
| 201 | + //if a theme has a png but not an svg always use the png |
|
| 202 | + $basename = substr(basename($file), 0, -4); |
|
| 203 | + |
|
| 204 | + try { |
|
| 205 | + $appPath = $this->getAppManager()->getAppPath($appName); |
|
| 206 | + } catch (AppPathNotFoundException $e) { |
|
| 207 | + if ($appName === 'core' || $appName === '') { |
|
| 208 | + $appName = 'core'; |
|
| 209 | + $appPath = false; |
|
| 210 | + } else { |
|
| 211 | + throw new RuntimeException('image not found: image: ' . $file . ' webroot: ' . \OC::$WEBROOT . ' serverroot: ' . \OC::$SERVERROOT); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + // Check if the app is in the app folder |
|
| 216 | + $path = ''; |
|
| 217 | + $themingEnabled = $this->config->getSystemValue('installed', false) && $this->getAppManager()->isEnabledForUser('theming'); |
|
| 218 | + $themingImagePath = false; |
|
| 219 | + if ($themingEnabled) { |
|
| 220 | + $themingDefaults = \OC::$server->getThemingDefaults(); |
|
| 221 | + if ($themingDefaults instanceof ThemingDefaults) { |
|
| 222 | + $themingImagePath = $themingDefaults->replaceImagePath($appName, $file); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$file")) { |
|
| 227 | + $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$file"; |
|
| 228 | + } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.svg") |
|
| 229 | + && file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$appName/img/$basename.png")) { |
|
| 230 | + $path = \OC::$WEBROOT . "/themes/$theme/apps/$appName/img/$basename.png"; |
|
| 231 | + } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$file")) { |
|
| 232 | + $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$file"; |
|
| 233 | + } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.svg") |
|
| 234 | + && file_exists(\OC::$SERVERROOT . "/themes/$theme/$appName/img/$basename.png"))) { |
|
| 235 | + $path = \OC::$WEBROOT . "/themes/$theme/$appName/img/$basename.png"; |
|
| 236 | + } elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$file")) { |
|
| 237 | + $path = \OC::$WEBROOT . "/themes/$theme/core/img/$file"; |
|
| 238 | + } elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.svg") |
|
| 239 | + && file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$basename.png")) { |
|
| 240 | + $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
|
| 241 | + } elseif ($themingEnabled && $themingImagePath) { |
|
| 242 | + $path = $themingImagePath; |
|
| 243 | + } elseif ($appPath && file_exists($appPath . "/img/$file")) { |
|
| 244 | + $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$file"; |
|
| 245 | + } elseif ($appPath && !file_exists($appPath . "/img/$basename.svg") |
|
| 246 | + && file_exists($appPath . "/img/$basename.png")) { |
|
| 247 | + $path = $this->getAppManager()->getAppWebPath($appName) . "/img/$basename.png"; |
|
| 248 | + } elseif (!empty($appName) and file_exists(\OC::$SERVERROOT . "/$appName/img/$file")) { |
|
| 249 | + $path = \OC::$WEBROOT . "/$appName/img/$file"; |
|
| 250 | + } elseif (!empty($appName) and (!file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.svg") |
|
| 251 | + && file_exists(\OC::$SERVERROOT . "/$appName/img/$basename.png"))) { |
|
| 252 | + $path = \OC::$WEBROOT . "/$appName/img/$basename.png"; |
|
| 253 | + } elseif (file_exists(\OC::$SERVERROOT . "/core/img/$file")) { |
|
| 254 | + $path = \OC::$WEBROOT . "/core/img/$file"; |
|
| 255 | + } elseif (!file_exists(\OC::$SERVERROOT . "/core/img/$basename.svg") |
|
| 256 | + && file_exists(\OC::$SERVERROOT . "/core/img/$basename.png")) { |
|
| 257 | + $path = \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png"; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + if ($path !== '') { |
|
| 261 | + $cache->set($cacheKey, $path); |
|
| 262 | + return $path; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * Makes an URL absolute |
|
| 271 | + * @param string $url the url in the ownCloud host |
|
| 272 | + * @return string the absolute version of the url |
|
| 273 | + */ |
|
| 274 | + public function getAbsoluteURL(string $url): string { |
|
| 275 | + $separator = strpos($url, '/') === 0 ? '' : '/'; |
|
| 276 | + |
|
| 277 | + if (\OC::$CLI && !\defined('PHPUNIT_RUN')) { |
|
| 278 | + return rtrim($this->config->getSystemValue('overwrite.cli.url'), '/') . '/' . ltrim($url, '/'); |
|
| 279 | + } |
|
| 280 | + // The ownCloud web root can already be prepended. |
|
| 281 | + if (\OC::$WEBROOT !== '' && strpos($url, \OC::$WEBROOT) === 0) { |
|
| 282 | + $url = substr($url, \strlen(\OC::$WEBROOT)); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + return $this->getBaseUrl() . $separator . $url; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * @param string $key |
|
| 290 | + * @return string url to the online documentation |
|
| 291 | + */ |
|
| 292 | + public function linkToDocs(string $key): string { |
|
| 293 | + $theme = \OC::$server->getThemingDefaults(); |
|
| 294 | + return $theme->buildDocLinkToKey($key); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Returns the URL of the default page based on the system configuration |
|
| 299 | + * and the apps visible for the current user |
|
| 300 | + * @return string |
|
| 301 | + */ |
|
| 302 | + public function linkToDefaultPageUrl(): string { |
|
| 303 | + // Deny the redirect if the URL contains a @ |
|
| 304 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 305 | + if (isset($_REQUEST['redirect_url']) && strpos($_REQUEST['redirect_url'], '@') === false) { |
|
| 306 | + return $this->getAbsoluteURL(urldecode($_REQUEST['redirect_url'])); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + $defaultPage = $this->config->getAppValue('core', 'defaultpage'); |
|
| 310 | + if ($defaultPage) { |
|
| 311 | + return $this->getAbsoluteURL($defaultPage); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $appId = 'files'; |
|
| 315 | + $defaultApps = explode(',', $this->config->getSystemValue('defaultapp', 'dashboard,files')); |
|
| 316 | + |
|
| 317 | + $userId = $this->userSession->isLoggedIn() ? $this->userSession->getUser()->getUID() : null; |
|
| 318 | + if ($userId !== null) { |
|
| 319 | + $userDefaultApps = explode(',', $this->config->getUserValue($userId, 'core', 'defaultapp')); |
|
| 320 | + $defaultApps = array_filter(array_merge($userDefaultApps, $defaultApps)); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + // find the first app that is enabled for the current user |
|
| 324 | + foreach ($defaultApps as $defaultApp) { |
|
| 325 | + $defaultApp = \OC_App::cleanAppId(strip_tags($defaultApp)); |
|
| 326 | + if (\OC::$server->getAppManager()->isEnabledForUser($defaultApp)) { |
|
| 327 | + $appId = $defaultApp; |
|
| 328 | + break; |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + if ($this->config->getSystemValue('htaccess.IgnoreFrontController', false) === true |
|
| 333 | + || getenv('front_controller_active') === 'true') { |
|
| 334 | + return $this->getAbsoluteURL('/apps/' . $appId . '/'); |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + return $this->getAbsoluteURL('/index.php/apps/' . $appId . '/'); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @return string base url of the current request |
|
| 342 | + */ |
|
| 343 | + public function getBaseUrl(): string { |
|
| 344 | + // BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup. |
|
| 345 | + if ($this->baseUrl === null || $this->baseUrl === "http://" || $this->baseUrl === "https://") { |
|
| 346 | + $this->baseUrl = $this->request->getServerProtocol() . '://' . $this->request->getServerHost() . \OC::$WEBROOT; |
|
| 347 | + } |
|
| 348 | + return $this->baseUrl; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * @return string webroot part of the base url |
|
| 353 | + */ |
|
| 354 | + public function getWebroot(): string { |
|
| 355 | + return \OC::$WEBROOT; |
|
| 356 | + } |
|
| 357 | 357 | } |
@@ -39,62 +39,62 @@ |
||
| 39 | 39 | |
| 40 | 40 | class HelpController extends Controller { |
| 41 | 41 | |
| 42 | - /** @var INavigationManager */ |
|
| 43 | - private $navigationManager; |
|
| 44 | - /** @var IURLGenerator */ |
|
| 45 | - private $urlGenerator; |
|
| 46 | - /** @var IGroupManager */ |
|
| 47 | - private $groupManager; |
|
| 42 | + /** @var INavigationManager */ |
|
| 43 | + private $navigationManager; |
|
| 44 | + /** @var IURLGenerator */ |
|
| 45 | + private $urlGenerator; |
|
| 46 | + /** @var IGroupManager */ |
|
| 47 | + private $groupManager; |
|
| 48 | 48 | |
| 49 | - /** @var string */ |
|
| 50 | - private $userId; |
|
| 49 | + /** @var string */ |
|
| 50 | + private $userId; |
|
| 51 | 51 | |
| 52 | - public function __construct( |
|
| 53 | - string $appName, |
|
| 54 | - IRequest $request, |
|
| 55 | - INavigationManager $navigationManager, |
|
| 56 | - IURLGenerator $urlGenerator, |
|
| 57 | - ?string $userId, |
|
| 58 | - IGroupManager $groupManager |
|
| 59 | - ) { |
|
| 60 | - parent::__construct($appName, $request); |
|
| 61 | - $this->navigationManager = $navigationManager; |
|
| 62 | - $this->urlGenerator = $urlGenerator; |
|
| 63 | - $this->userId = $userId; |
|
| 64 | - $this->groupManager = $groupManager; |
|
| 65 | - } |
|
| 52 | + public function __construct( |
|
| 53 | + string $appName, |
|
| 54 | + IRequest $request, |
|
| 55 | + INavigationManager $navigationManager, |
|
| 56 | + IURLGenerator $urlGenerator, |
|
| 57 | + ?string $userId, |
|
| 58 | + IGroupManager $groupManager |
|
| 59 | + ) { |
|
| 60 | + parent::__construct($appName, $request); |
|
| 61 | + $this->navigationManager = $navigationManager; |
|
| 62 | + $this->urlGenerator = $urlGenerator; |
|
| 63 | + $this->userId = $userId; |
|
| 64 | + $this->groupManager = $groupManager; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @return TemplateResponse |
|
| 69 | - * |
|
| 70 | - * @NoCSRFRequired |
|
| 71 | - * @NoAdminRequired |
|
| 72 | - * @NoSubAdminRequired |
|
| 73 | - */ |
|
| 74 | - public function help(string $mode = 'user'): TemplateResponse { |
|
| 75 | - $this->navigationManager->setActiveEntry('help'); |
|
| 67 | + /** |
|
| 68 | + * @return TemplateResponse |
|
| 69 | + * |
|
| 70 | + * @NoCSRFRequired |
|
| 71 | + * @NoAdminRequired |
|
| 72 | + * @NoSubAdminRequired |
|
| 73 | + */ |
|
| 74 | + public function help(string $mode = 'user'): TemplateResponse { |
|
| 75 | + $this->navigationManager->setActiveEntry('help'); |
|
| 76 | 76 | |
| 77 | - if ($mode !== 'admin') { |
|
| 78 | - $mode = 'user'; |
|
| 79 | - } |
|
| 77 | + if ($mode !== 'admin') { |
|
| 78 | + $mode = 'user'; |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $documentationUrl = $this->urlGenerator->getAbsoluteURL( |
|
| 82 | - $this->urlGenerator->linkTo('', 'core/doc/' . $mode . '/index.html') |
|
| 83 | - ); |
|
| 81 | + $documentationUrl = $this->urlGenerator->getAbsoluteURL( |
|
| 82 | + $this->urlGenerator->linkTo('', 'core/doc/' . $mode . '/index.html') |
|
| 83 | + ); |
|
| 84 | 84 | |
| 85 | - $urlUserDocs = $this->urlGenerator->linkToRoute('settings.Help.help', ['mode' => 'user']); |
|
| 86 | - $urlAdminDocs = $this->urlGenerator->linkToRoute('settings.Help.help', ['mode' => 'admin']); |
|
| 85 | + $urlUserDocs = $this->urlGenerator->linkToRoute('settings.Help.help', ['mode' => 'user']); |
|
| 86 | + $urlAdminDocs = $this->urlGenerator->linkToRoute('settings.Help.help', ['mode' => 'admin']); |
|
| 87 | 87 | |
| 88 | - $response = new TemplateResponse('settings', 'help', [ |
|
| 89 | - 'admin' => $this->groupManager->isAdmin($this->userId), |
|
| 90 | - 'url' => $documentationUrl, |
|
| 91 | - 'urlUserDocs' => $urlUserDocs, |
|
| 92 | - 'urlAdminDocs' => $urlAdminDocs, |
|
| 93 | - 'mode' => $mode, |
|
| 94 | - ]); |
|
| 95 | - $policy = new ContentSecurityPolicy(); |
|
| 96 | - $policy->addAllowedFrameDomain('\'self\''); |
|
| 97 | - $response->setContentSecurityPolicy($policy); |
|
| 98 | - return $response; |
|
| 99 | - } |
|
| 88 | + $response = new TemplateResponse('settings', 'help', [ |
|
| 89 | + 'admin' => $this->groupManager->isAdmin($this->userId), |
|
| 90 | + 'url' => $documentationUrl, |
|
| 91 | + 'urlUserDocs' => $urlUserDocs, |
|
| 92 | + 'urlAdminDocs' => $urlAdminDocs, |
|
| 93 | + 'mode' => $mode, |
|
| 94 | + ]); |
|
| 95 | + $policy = new ContentSecurityPolicy(); |
|
| 96 | + $policy->addAllowedFrameDomain('\'self\''); |
|
| 97 | + $response->setContentSecurityPolicy($policy); |
|
| 98 | + return $response; |
|
| 99 | + } |
|
| 100 | 100 | } |