DocSpace-client/common/ASC.Core.Common/BaseCommonLinkUtility.cs

257 lines
9.4 KiB
C#
Raw Normal View History

2019-11-06 15:03:09 +00:00
/*
*
* (c) Copyright Ascensio System Limited 2010-2018
*
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
*
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
*
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
*
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
*
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
* relevant author attributions when distributing the software. If the display of the logo in its graphic
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
* in every copy of the program you distribute.
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
*/
namespace ASC.Core.Common
2019-11-01 08:49:08 +00:00
{
2021-05-30 18:52:32 +00:00
[Scope]
2019-11-01 08:49:08 +00:00
public class CommonLinkUtilitySettings
{
public string ServerUri { get; set; }
}
2019-11-06 15:03:09 +00:00
2020-10-19 15:53:15 +00:00
[Scope]
2019-11-06 15:03:09 +00:00
public class BaseCommonLinkUtility
{
private const string LocalHost = "localhost";
2019-11-06 15:03:09 +00:00
private UriBuilder _serverRoot;
private string _vpath;
2019-09-19 15:55:44 +00:00
protected IHttpContextAccessor HttpContextAccessor;
2019-11-06 15:03:09 +00:00
2019-10-17 15:55:35 +00:00
public BaseCommonLinkUtility(
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
TenantManager tenantManager,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options,
2020-10-30 14:57:31 +00:00
CommonLinkUtilitySettings settings)
2019-11-06 15:03:09 +00:00
: this(null, coreBaseSettings, coreSettings, tenantManager, options, settings)
2019-09-19 15:55:44 +00:00
{
}
2019-11-06 15:03:09 +00:00
2019-10-17 15:55:35 +00:00
public BaseCommonLinkUtility(
IHttpContextAccessor httpContextAccessor,
2019-10-10 10:59:22 +00:00
CoreBaseSettings coreBaseSettings,
CoreSettings coreSettings,
2019-10-17 15:55:35 +00:00
TenantManager tenantManager,
2019-11-06 15:03:09 +00:00
IOptionsMonitor<ILog> options,
2020-10-30 14:57:31 +00:00
CommonLinkUtilitySettings settings)
2019-11-01 08:49:08 +00:00
{
2020-10-30 14:57:31 +00:00
var serverUri = settings.ServerUri;
2019-11-01 08:49:08 +00:00
2019-11-06 15:03:09 +00:00
if (!string.IsNullOrEmpty(serverUri))
2019-09-19 15:55:44 +00:00
{
2019-11-01 08:49:08 +00:00
var uri = new Uri(serverUri.Replace('*', 'x').Replace('+', 'x'));
_serverRoot = new UriBuilder(uri.Scheme, uri.Host != "x" ? uri.Host : LocalHost, uri.Port);
2019-11-01 08:49:08 +00:00
_vpath = "/" + uri.AbsolutePath.Trim('/');
}
else
{
try
{
2020-05-17 13:08:20 +00:00
HttpContextAccessor = httpContextAccessor;
var uriBuilder = new UriBuilder(Uri.UriSchemeHttp, LocalHost);
2020-05-17 13:08:20 +00:00
if (HttpContextAccessor?.HttpContext?.Request != null)
2019-11-01 08:49:08 +00:00
{
2020-05-17 13:08:20 +00:00
var u = HttpContextAccessor?.HttpContext.Request.GetUrlRewriter();
2021-12-29 15:47:35 +00:00
if (u == null)
{
throw new ArgumentNullException(nameof(u));
}
2021-12-29 15:47:35 +00:00
uriBuilder = new UriBuilder(u.Scheme, LocalHost, u.Port);
2019-11-01 08:49:08 +00:00
}
_serverRoot = uriBuilder;
}
catch (Exception error)
{
options.Get("ASC.Web").Error(error);
}
2019-09-19 15:55:44 +00:00
}
CoreBaseSettings = coreBaseSettings;
_coreSettings = coreSettings;
2019-09-19 15:55:44 +00:00
TenantManager = tenantManager;
2019-11-06 15:03:09 +00:00
}
public string VirtualRoot => ToAbsolute("~");
2019-09-19 15:55:44 +00:00
protected CoreBaseSettings CoreBaseSettings;
private readonly CoreSettings _coreSettings;
protected TenantManager TenantManager;
2019-09-19 15:55:44 +00:00
private string _serverRootPath;
2019-11-06 15:03:09 +00:00
public string ServerRootPath
2019-08-08 09:26:58 +00:00
{
2019-09-19 15:55:44 +00:00
get
2019-08-08 09:26:58 +00:00
{
if (!string.IsNullOrEmpty(_serverRootPath))
{
return _serverRootPath;
}
2019-09-19 15:55:44 +00:00
UriBuilder result;
// first, take from current request
2020-05-17 13:08:20 +00:00
if (HttpContextAccessor?.HttpContext?.Request != null)
2019-09-19 15:55:44 +00:00
{
2020-05-17 13:08:20 +00:00
var u = HttpContextAccessor?.HttpContext?.Request.GetUrlRewriter();
2021-12-29 15:47:35 +00:00
if (u == null)
{
throw new ArgumentNullException(nameof(u));
}
2021-12-29 15:47:35 +00:00
2019-09-19 15:55:44 +00:00
result = new UriBuilder(u.Scheme, u.Host, u.Port);
2019-08-08 09:26:58 +00:00
2019-09-19 15:55:44 +00:00
if (CoreBaseSettings.Standalone && !result.Uri.IsLoopback)
{
// save for stanalone
_serverRoot.Host = result.Host;
}
}
else
2019-08-08 09:26:58 +00:00
{
2019-09-19 15:55:44 +00:00
result = new UriBuilder(_serverRoot.Uri);
2019-08-08 09:26:58 +00:00
}
2019-09-19 15:55:44 +00:00
if (result.Uri.IsLoopback)
{
// take values from db if localhost or no http context thread
var tenant = TenantManager.GetCurrentTenant();
result.Host = tenant.GetTenantDomain(_coreSettings);
2019-08-08 09:26:58 +00:00
2019-11-06 15:03:09 +00:00
#if DEBUG
2019-09-19 15:55:44 +00:00
// for Visual Studio debug
if (tenant.Alias == LocalHost)
2019-09-19 15:55:44 +00:00
{
result.Host = LocalHost;
2019-09-19 15:55:44 +00:00
}
2019-11-06 15:03:09 +00:00
#endif
2019-08-08 09:26:58 +00:00
2019-09-19 15:55:44 +00:00
if (!string.IsNullOrEmpty(tenant.MappedDomain))
2019-08-08 09:26:58 +00:00
{
2019-09-19 15:55:44 +00:00
var mapped = tenant.MappedDomain.ToLowerInvariant();
if (!mapped.Contains(Uri.SchemeDelimiter))
{
mapped = Uri.UriSchemeHttp + Uri.SchemeDelimiter + mapped;
}
result = new UriBuilder(mapped);
2019-08-08 09:26:58 +00:00
}
}
return _serverRootPath = result.Uri.ToString().TrimEnd('/');
2019-11-06 15:03:09 +00:00
}
}
public string GetFullAbsolutePath(string virtualPath)
{
2019-09-19 15:55:44 +00:00
if (string.IsNullOrEmpty(virtualPath))
{
return ServerRootPath;
2019-11-06 15:03:09 +00:00
}
2019-11-06 15:03:09 +00:00
if (virtualPath.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) ||
virtualPath.StartsWith("mailto:", StringComparison.InvariantCultureIgnoreCase) ||
virtualPath.StartsWith("javascript:", StringComparison.InvariantCultureIgnoreCase) ||
virtualPath.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
{
2019-11-06 15:03:09 +00:00
return virtualPath;
}
2019-11-06 15:03:09 +00:00
2022-01-24 09:28:00 +00:00
if (virtualPath.StartsWith('/'))
2019-11-06 15:03:09 +00:00
{
return ServerRootPath + virtualPath;
}
2019-11-06 15:03:09 +00:00
return ServerRootPath + VirtualRoot.TrimEnd('/') + "/" + virtualPath.TrimStart('~', '/');
}
2020-02-05 13:33:09 +00:00
public string ToAbsolute(string virtualPath)
2019-11-06 15:03:09 +00:00
{
if (_vpath == null)
{
return VirtualPathUtility.ToAbsolute(virtualPath);
}
2022-01-18 12:40:28 +00:00
if (string.IsNullOrEmpty(virtualPath) || virtualPath.StartsWith('/'))
2019-11-06 15:03:09 +00:00
{
return virtualPath;
}
2019-11-06 15:03:09 +00:00
return (_vpath != "/" ? _vpath : string.Empty) + "/" + virtualPath.TrimStart('~', '/');
2019-08-19 13:51:36 +00:00
}
public static string GetRegionalUrl(string url, string lang)
{
if (string.IsNullOrEmpty(url))
{
2019-08-19 13:51:36 +00:00
return url;
}
2019-08-19 13:51:36 +00:00
//-replace language
var regex = new Regex("{.*?}");
var matches = regex.Matches(url);
if (string.IsNullOrEmpty(lang))
{
2022-01-11 15:37:10 +00:00
url = matches.Aggregate(url, (current, match) => current.Replace(match.Value, string.Empty));
2019-08-19 13:51:36 +00:00
}
else
{
foreach (Match match in matches)
{
var values = match.Value.TrimStart('{').TrimEnd('}').Split('|');
url = url.Replace(match.Value, values.Contains(lang) ? lang : string.Empty);
}
}
//-
//--remove redundant slashes
var uri = new Uri(url);
2020-09-18 07:59:23 +00:00
if (uri.Scheme == "mailto")
{
2020-09-18 07:59:23 +00:00
return uri.OriginalString;
}
2020-09-18 07:59:23 +00:00
2019-08-19 13:51:36 +00:00
var baseUri = new UriBuilder(uri.Scheme, uri.Host, uri.Port).Uri;
baseUri = uri.Segments.Aggregate(baseUri, (current, segment) => new Uri(current, segment));
//--
//todo: lost query string!!!
return baseUri.ToString().TrimEnd('/');
2019-11-06 15:03:09 +00:00
}
2020-06-03 14:53:58 +00:00
2021-05-17 11:35:00 +00:00
public void Initialize(string serverUri, bool localhost = true)
2020-06-03 14:53:58 +00:00
{
var uri = new Uri(serverUri.Replace('*', 'x').Replace('+', 'x'));
_serverRoot = new UriBuilder(uri.Scheme, localhost ? LocalHost : uri.Host, uri.Port);
2020-06-03 14:53:58 +00:00
_vpath = "/" + uri.AbsolutePath.Trim('/');
}
2019-10-31 11:28:30 +00:00
}
2019-11-06 15:03:09 +00:00
}