2013-09-26 20:41:19 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
|
|
|
*
|
2016-01-12 17:02:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 13:44:34 +03:00
|
|
|
* @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/>
|
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;
|
2014-11-24 18:24:26 +03:00
|
|
|
use OC_Defaults;
|
2015-03-17 14:35:47 +03:00
|
|
|
use OCP\ICacheFactory;
|
|
|
|
use OCP\IConfig;
|
2013-10-07 02:32:39 +04:00
|
|
|
use OCP\IURLGenerator;
|
|
|
|
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;
|
2013-12-31 17:36:02 +04:00
|
|
|
|
|
|
|
/**
|
2015-03-17 14:35:47 +03:00
|
|
|
* @param IConfig $config
|
|
|
|
* @param ICacheFactory $cacheFactory
|
2013-12-31 17:36:02 +04:00
|
|
|
*/
|
2015-03-17 14:35:47 +03:00
|
|
|
public function __construct(IConfig $config,
|
|
|
|
ICacheFactory $cacheFactory) {
|
2013-12-31 17:36:02 +04:00
|
|
|
$this->config = $config;
|
2015-03-17 14:35:47 +03:00
|
|
|
$this->cacheFactory = $cacheFactory;
|
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
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $route
|
2015-02-12 18:35:47 +03:00
|
|
|
* @param array $parameters 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
|
|
|
*/
|
|
|
|
public function linkToRoute($route, $parameters = array()) {
|
2015-11-25 18:58:54 +03:00
|
|
|
// TODO: mock router
|
2014-03-10 17:06:47 +04:00
|
|
|
$urlLinkTo = \OC::$server->getRouter()->generate($route, $parameters);
|
2013-09-26 20:41:19 +04:00
|
|
|
return $urlLinkTo;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public function linkToRouteAbsolute($routeName, $arguments = array()) {
|
|
|
|
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
|
|
|
|
}
|
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* Creates an url
|
2013-09-26 20:41:19 +04:00
|
|
|
* @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() ) {
|
2015-02-11 03:10:03 +03:00
|
|
|
$frontControllerActive = (getenv('front_controller_active') === 'true');
|
2013-12-31 17:36:02 +04:00
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
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)) {
|
2014-03-26 18:36:55 +04:00
|
|
|
if (substr($file, -3) == 'php') {
|
2013-12-31 17:36:02 +04:00
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/index.php/apps/' . $app;
|
2013-12-31 17:36:02 +04:00
|
|
|
if ($frontControllerActive) {
|
|
|
|
$urlLinkTo = \OC::$WEBROOT . '/apps/' . $app;
|
|
|
|
}
|
2013-09-26 20:41:19 +04:00
|
|
|
$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 {
|
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
|
2013-09-26 20:41:19 +04:00
|
|
|
* @param string $app app
|
|
|
|
* @param string $image 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.
|
|
|
|
*/
|
|
|
|
public function imagePath($app, $image) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$cache = $this->cacheFactory->create('imagePath');
|
|
|
|
$cacheKey = $app.'-'.$image;
|
2015-03-17 15:15:52 +03:00
|
|
|
if($key = $cache->get($cacheKey)) {
|
|
|
|
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
|
|
|
|
$basename = substr(basename($image),0,-4);
|
|
|
|
|
2015-04-04 01:42:18 +03:00
|
|
|
$appPath = \OC_App::getAppPath($app);
|
|
|
|
|
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 = '';
|
2013-09-26 20:41:19 +04:00
|
|
|
if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$image";
|
2013-10-23 18:44:56 +04:00
|
|
|
} elseif (!file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.svg")
|
2013-10-28 17:18:20 +04:00
|
|
|
&& file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$basename.png")) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png";
|
2013-09-26 20:41:19 +04:00
|
|
|
} elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$image")) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = \OC::$WEBROOT . "/themes/$theme/$app/img/$image";
|
2013-10-23 18:44:56 +04:00
|
|
|
} elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.svg")
|
2013-10-28 17:18:20 +04:00
|
|
|
&& file_exists(\OC::$SERVERROOT . "/themes/$theme/$app/img/$basename.png"))) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png";
|
2013-09-26 20:41:19 +04:00
|
|
|
} elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = \OC::$WEBROOT . "/themes/$theme/core/img/$image";
|
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";
|
2016-04-04 13:57:52 +03:00
|
|
|
} elseif ($appPath && file_exists($appPath . "/img/$image")) {
|
|
|
|
$path = \OC_App::getAppWebPath($app) . "/img/$image";
|
|
|
|
} elseif ($appPath && !file_exists($appPath . "/img/$basename.svg")
|
|
|
|
&& file_exists($appPath . "/img/$basename.png")) {
|
|
|
|
$path = \OC_App::getAppWebPath($app) . "/img/$basename.png";
|
|
|
|
} elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) {
|
|
|
|
$path = \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"))) {
|
|
|
|
$path = \OC::$WEBROOT . "/$app/img/$basename.png";
|
2013-09-26 20:41:19 +04:00
|
|
|
} elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) {
|
2015-03-17 14:35:47 +03:00
|
|
|
$path = \OC::$WEBROOT . "/core/img/$image";
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if($path !== '') {
|
|
|
|
$cache->set($cacheKey, $path);
|
|
|
|
return $path;
|
2013-09-26 20:41:19 +04:00
|
|
|
} else {
|
|
|
|
throw new RuntimeException('image not found: image:' . $image . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2013-10-07 02:32:39 +04:00
|
|
|
public function getAbsoluteURL($url) {
|
2014-02-07 20:39:19 +04:00
|
|
|
$separator = $url[0] === '/' ? '' : '/';
|
2014-03-07 02:56:11 +04:00
|
|
|
|
2014-10-13 13:18:24 +04:00
|
|
|
if (\OC::$CLI && !defined('PHPUNIT_RUN')) {
|
|
|
|
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.
|
|
|
|
$webRoot = substr($url, 0, strlen(\OC::$WEBROOT)) === \OC::$WEBROOT
|
|
|
|
? ''
|
|
|
|
: \OC::$WEBROOT;
|
|
|
|
|
2015-02-10 15:02:48 +03:00
|
|
|
$request = \OC::$server->getRequest();
|
|
|
|
return $request->getServerProtocol() . '://' . $request->getServerHost() . $webRoot . $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
|
|
|
|
*/
|
|
|
|
public function linkToDocs($key) {
|
|
|
|
$theme = new OC_Defaults();
|
|
|
|
return $theme->buildDocLinkToKey($key);
|
|
|
|
}
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|