2013-09-26 20:41:19 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC;
|
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 {
|
2013-12-31 17:36:02 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\IConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OCP\IConfig $config
|
|
|
|
*/
|
|
|
|
public function __construct($config) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
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
|
2013-09-26 20:41:19 +04:00
|
|
|
* @param array $parameters
|
|
|
|
* @internal param array $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()) {
|
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
|
|
|
|
* @param string $route
|
|
|
|
* @param array $parameters
|
|
|
|
* @internal param array $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($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() ) {
|
2013-12-31 17:36:02 +04:00
|
|
|
$frontControllerActive=($this->config->getSystemValue('front_controller_active', 'false') == 'true');
|
|
|
|
|
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') {
|
|
|
|
$urlLinkTo = \OC::$WEBROOT;
|
|
|
|
} 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) {
|
|
|
|
// 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);
|
|
|
|
|
2013-09-26 20:41:19 +04:00
|
|
|
// Check if the app is in the app folder
|
|
|
|
if (file_exists(\OC::$SERVERROOT . "/themes/$theme/apps/$app/img/$image")) {
|
|
|
|
return \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")) {
|
2013-10-23 18:44:56 +04:00
|
|
|
return \OC::$WEBROOT . "/themes/$theme/apps/$app/img/$basename.png";
|
2013-09-26 20:41:19 +04:00
|
|
|
} elseif (file_exists(\OC_App::getAppPath($app) . "/img/$image")) {
|
|
|
|
return \OC_App::getAppWebPath($app) . "/img/$image";
|
2013-10-23 18:44:56 +04:00
|
|
|
} elseif (!file_exists(\OC_App::getAppPath($app) . "/img/$basename.svg")
|
2013-10-28 17:18:20 +04:00
|
|
|
&& file_exists(\OC_App::getAppPath($app) . "/img/$basename.png")) {
|
2013-10-23 18:44:56 +04:00
|
|
|
return \OC_App::getAppPath($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")) {
|
|
|
|
return \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"))) {
|
2013-10-23 18:44:56 +04:00
|
|
|
return \OC::$WEBROOT . "/themes/$theme/$app/img/$basename.png";
|
2013-09-26 20:41:19 +04:00
|
|
|
} elseif (!empty($app) and file_exists(\OC::$SERVERROOT . "/$app/img/$image")) {
|
|
|
|
return \OC::$WEBROOT . "/$app/img/$image";
|
2013-10-23 18:44:56 +04:00
|
|
|
} elseif (!empty($app) and (!file_exists(\OC::$SERVERROOT . "/$app/img/$basename.svg")
|
2013-10-28 17:18:20 +04:00
|
|
|
&& file_exists(\OC::$SERVERROOT . "/$app/img/$basename.png"))) {
|
2013-10-23 18:44:56 +04:00
|
|
|
return \OC::$WEBROOT . "/$app/img/$basename.png";
|
2013-09-26 20:41:19 +04:00
|
|
|
} elseif (file_exists(\OC::$SERVERROOT . "/themes/$theme/core/img/$image")) {
|
|
|
|
return \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")) {
|
2013-10-23 18:44:56 +04:00
|
|
|
return \OC::$WEBROOT . "/themes/$theme/core/img/$basename.png";
|
2013-09-26 20:41:19 +04:00
|
|
|
} elseif (file_exists(\OC::$SERVERROOT . "/core/img/$image")) {
|
|
|
|
return \OC::$WEBROOT . "/core/img/$image";
|
|
|
|
} 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
|
|
|
|
* @param string $url the url in the owncloud host
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost(). $webRoot . $separator . $url;
|
2013-09-26 20:41:19 +04:00
|
|
|
}
|
|
|
|
}
|