2019-04-08 15:12:10 -04:00
|
|
|
<?php
|
2020-02-08 11:16:42 -05:00
|
|
|
/**
|
2023-01-01 09:36:24 -05:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2020-02-08 11:16:42 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-04-08 15:12:10 -04:00
|
|
|
|
2019-08-15 11:23:00 -04:00
|
|
|
namespace Friendica\App;
|
2019-04-08 15:12:10 -04:00
|
|
|
|
2021-10-26 15:44:29 -04:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
2019-12-03 15:11:42 -05:00
|
|
|
use Friendica\Core\System;
|
2019-08-15 11:23:00 -04:00
|
|
|
use Friendica\Util\Strings;
|
2019-12-03 15:11:42 -05:00
|
|
|
use Friendica\Network\HTTPException;
|
2023-02-18 14:24:48 -05:00
|
|
|
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
|
use GuzzleHttp\Psr7\Uri;
|
|
|
|
use Psr\Http\Message\UriInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2019-04-08 15:12:10 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A class which checks and contains the basic
|
2019-04-08 17:12:34 -04:00
|
|
|
* environment for the BaseURL (url, urlpath, ssl_policy, hostname, scheme)
|
2019-04-08 15:12:10 -04:00
|
|
|
*/
|
2023-02-18 14:24:48 -05:00
|
|
|
class BaseURL extends Uri implements UriInterface
|
2019-04-08 15:12:10 -04:00
|
|
|
{
|
2023-02-18 14:24:48 -05:00
|
|
|
public function __construct(IManageConfigValues $config, LoggerInterface $logger, array $server = [])
|
2019-04-08 15:12:10 -04:00
|
|
|
{
|
2023-02-18 14:24:48 -05:00
|
|
|
$url = $config->get('system', 'url');
|
|
|
|
if (empty($url)) {
|
|
|
|
$logger->critical('Invalid config - Missing system.url');
|
|
|
|
$url = ServerRequest::getUriFromGlobals()
|
|
|
|
->withQuery('')
|
|
|
|
->withPath($this->determineURLPath($server));
|
2023-01-04 02:14:00 -05:00
|
|
|
}
|
2023-02-19 05:24:13 -05:00
|
|
|
|
|
|
|
parent::__construct($url);
|
2019-04-08 15:12:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-18 14:24:48 -05:00
|
|
|
* Figure out if we are running at the top of a domain or in a subdirectory
|
2019-04-08 15:12:10 -04:00
|
|
|
*/
|
2023-02-18 14:24:48 -05:00
|
|
|
private function determineURLPath(array $server): string
|
2019-04-08 15:12:10 -04:00
|
|
|
{
|
2023-02-18 14:24:48 -05:00
|
|
|
/* Relative script path to the web server root
|
|
|
|
* Not all of those $_SERVER properties can be present, so we do by inverse priority order
|
|
|
|
*/
|
|
|
|
$relativeScriptPath =
|
|
|
|
($server['REDIRECT_URL'] ?? '') ?:
|
|
|
|
($server['REDIRECT_URI'] ?? '') ?:
|
|
|
|
($server['REDIRECT_SCRIPT_URL'] ?? '') ?:
|
|
|
|
($server['SCRIPT_URL'] ?? '') ?:
|
|
|
|
$server['REQUEST_URI'] ?? '';
|
|
|
|
|
|
|
|
/* $relativeScriptPath gives /relative/path/to/friendica/module/parameter
|
|
|
|
* QUERY_STRING gives pagename=module/parameter
|
|
|
|
*
|
|
|
|
* To get /relative/path/to/friendica we perform dirname() for as many levels as there are slashes in the QUERY_STRING
|
|
|
|
*/
|
|
|
|
if (!empty($relativeScriptPath)) {
|
|
|
|
// Module
|
|
|
|
if (!empty($server['QUERY_STRING'])) {
|
|
|
|
return trim(dirname($relativeScriptPath, substr_count(trim($server['QUERY_STRING'], '/'), '/') + 1), '/');
|
|
|
|
} else {
|
|
|
|
// Root page
|
|
|
|
$scriptPathParts = explode('?', $relativeScriptPath, 2);
|
|
|
|
return trim($scriptPathParts[0], '/');
|
2019-04-08 17:12:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 14:24:48 -05:00
|
|
|
return '';
|
2019-04-08 15:12:10 -04:00
|
|
|
}
|
2019-08-15 11:23:00 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the base url from an url. This avoids some mixed content problems.
|
|
|
|
*
|
|
|
|
* @param string $origURL
|
|
|
|
*
|
|
|
|
* @return string The cleaned url
|
|
|
|
*/
|
2022-06-16 10:49:43 -04:00
|
|
|
public function remove(string $origURL): string
|
2019-08-15 11:23:00 -04:00
|
|
|
{
|
|
|
|
// Remove the hostname from the url if it is an internal link
|
|
|
|
$nurl = Strings::normaliseLink($origURL);
|
2023-02-18 14:24:48 -05:00
|
|
|
$base = Strings::normaliseLink($this->__toString());
|
2019-08-15 11:23:00 -04:00
|
|
|
$url = str_replace($base . '/', '', $nurl);
|
|
|
|
|
2023-02-18 14:24:48 -05:00
|
|
|
// if it is an external link return the original value
|
|
|
|
if ($url === $nurl) {
|
2019-08-15 11:23:00 -04:00
|
|
|
return $origURL;
|
|
|
|
} else {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
}
|
2019-12-03 15:11:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirects to another module relative to the current Friendica base URL.
|
|
|
|
* If you want to redirect to a external URL, use System::externalRedirectTo()
|
|
|
|
*
|
|
|
|
* @param string $toUrl The destination URL (Default is empty, which is the default page of the Friendica node)
|
|
|
|
* @param bool $ssl if true, base URL will try to get called with https:// (works just for relative paths)
|
|
|
|
*
|
2022-07-07 15:47:39 -04:00
|
|
|
* @throws HTTPException\FoundException
|
|
|
|
* @throws HTTPException\MovedPermanentlyException
|
|
|
|
* @throws HTTPException\TemporaryRedirectException
|
|
|
|
*
|
2019-12-03 15:11:42 -05:00
|
|
|
* @throws HTTPException\InternalServerErrorException In Case the given URL is not relative to the Friendica node
|
|
|
|
*/
|
2022-06-16 10:49:43 -04:00
|
|
|
public function redirect(string $toUrl = '', bool $ssl = false)
|
2019-12-03 15:11:42 -05:00
|
|
|
{
|
|
|
|
if (!empty(parse_url($toUrl, PHP_URL_SCHEME))) {
|
2023-02-04 19:18:05 -05:00
|
|
|
throw new HTTPException\InternalServerErrorException("$toUrl is not a relative path, please use System::externalRedirectTo");
|
2019-12-03 15:11:42 -05:00
|
|
|
}
|
|
|
|
|
2023-02-18 14:24:48 -05:00
|
|
|
$redirectTo = $this->__toString() . '/' . ltrim($toUrl, '/');
|
2019-12-03 15:11:42 -05:00
|
|
|
System::externalRedirect($redirectTo);
|
|
|
|
}
|
2019-04-08 15:12:10 -04:00
|
|
|
}
|