2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
2019-11-06 15:03:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
namespace ASC.Core.Common;
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
public class CommonLinkUtilitySettings
|
2019-11-01 08:49:08 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
public string ServerUri { get; set; }
|
|
|
|
}
|
2019-11-01 08:49:08 +00:00
|
|
|
|
2019-11-06 15:03:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
[Scope]
|
|
|
|
public class BaseCommonLinkUtility
|
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
private const string LocalHost = "localhost";
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
private UriBuilder _serverRoot;
|
|
|
|
private string _vpath;
|
2019-11-06 15:03:09 +00:00
|
|
|
|
2022-03-25 16:26:06 +00:00
|
|
|
protected IHttpContextAccessor _httpContextAccessor;
|
2019-09-19 15:55:44 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public BaseCommonLinkUtility(
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
CoreSettings coreSettings,
|
|
|
|
TenantManager tenantManager,
|
2022-04-26 17:53:48 +00:00
|
|
|
ILoggerProvider options,
|
2022-02-15 11:52:43 +00:00
|
|
|
CommonLinkUtilitySettings settings)
|
|
|
|
: this(null, coreBaseSettings, coreSettings, tenantManager, options, settings)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public BaseCommonLinkUtility(
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
|
|
|
CoreSettings coreSettings,
|
|
|
|
TenantManager tenantManager,
|
2022-04-26 17:53:48 +00:00
|
|
|
ILoggerProvider options,
|
2022-02-15 11:52:43 +00:00
|
|
|
CommonLinkUtilitySettings settings)
|
|
|
|
{
|
|
|
|
var serverUri = settings.ServerUri;
|
2019-11-06 15:03:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(serverUri))
|
2019-09-19 15:55:44 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
var uri = new Uri(serverUri.Replace('*', 'x').Replace('+', 'x'));
|
2022-03-25 16:26:06 +00:00
|
|
|
_serverRoot = new UriBuilder(uri.Scheme, uri.Host != "x" ? uri.Host : LocalHost, uri.Port);
|
2022-02-15 11:52:43 +00:00
|
|
|
_vpath = "/" + uri.AbsolutePath.Trim('/');
|
2019-09-19 15:55:44 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
else
|
2019-11-01 08:49:08 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
try
|
2019-11-01 08:49:08 +00:00
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
|
var uriBuilder = new UriBuilder(Uri.UriSchemeHttp, LocalHost);
|
|
|
|
if (_httpContextAccessor?.HttpContext?.Request != null)
|
2019-11-01 08:49:08 +00:00
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
var u = _httpContextAccessor?.HttpContext.Request.GetUrlRewriter();
|
2021-12-29 15:47:35 +00:00
|
|
|
|
2022-03-09 17:15:51 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(u);
|
2022-02-15 11:52:43 +00:00
|
|
|
|
2022-03-25 16:26:06 +00:00
|
|
|
uriBuilder = new UriBuilder(u.Scheme, LocalHost, u.Port);
|
2019-11-01 08:49:08 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
_serverRoot = uriBuilder;
|
|
|
|
}
|
|
|
|
catch (Exception error)
|
|
|
|
{
|
2022-05-06 07:58:15 +00:00
|
|
|
options.CreateLogger("ASC.Web").ErrorWithException(error);
|
2019-09-19 15:55:44 +00:00
|
|
|
}
|
2019-11-06 15:03:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 16:26:06 +00:00
|
|
|
_coreBaseSettings = coreBaseSettings;
|
2022-02-15 11:52:43 +00:00
|
|
|
_coreSettings = coreSettings;
|
2022-03-25 16:26:06 +00:00
|
|
|
_tenantManager = tenantManager;
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
2019-09-19 15:55:44 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public string VirtualRoot => ToAbsolute("~");
|
2019-09-19 15:55:44 +00:00
|
|
|
|
2022-03-25 16:26:06 +00:00
|
|
|
protected CoreBaseSettings _coreBaseSettings;
|
2022-02-15 11:52:43 +00:00
|
|
|
private readonly CoreSettings _coreSettings;
|
2022-03-25 16:26:06 +00:00
|
|
|
protected TenantManager _tenantManager;
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
private string _serverRootPath;
|
|
|
|
public string ServerRootPath
|
|
|
|
{
|
|
|
|
get
|
2019-08-08 09:26:58 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(_serverRootPath))
|
2019-08-08 09:26:58 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
return _serverRootPath;
|
|
|
|
}
|
2022-02-14 21:02:57 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
UriBuilder result;
|
|
|
|
// first, take from current request
|
2022-03-25 16:26:06 +00:00
|
|
|
if (_httpContextAccessor?.HttpContext?.Request != null)
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
var u = _httpContextAccessor?.HttpContext?.Request.GetUrlRewriter();
|
2021-12-29 15:47:35 +00:00
|
|
|
|
2022-03-09 17:15:51 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(u);
|
2021-12-29 15:47:35 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
result = new UriBuilder(u.Scheme, u.Host, u.Port);
|
2019-08-08 09:26:58 +00:00
|
|
|
|
2022-03-25 16:26:06 +00:00
|
|
|
if (_coreBaseSettings.Standalone && !result.Uri.IsLoopback)
|
2019-08-08 09:26:58 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
// save for stanalone
|
|
|
|
_serverRoot.Host = result.Host;
|
2019-08-08 09:26:58 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = new UriBuilder(_serverRoot.Uri);
|
|
|
|
}
|
2019-08-08 09:26:58 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (result.Uri.IsLoopback)
|
|
|
|
{
|
|
|
|
// take values from db if localhost or no http context thread
|
2022-03-25 16:26:06 +00:00
|
|
|
var tenant = _tenantManager.GetCurrentTenant();
|
2022-02-15 11:52:43 +00:00
|
|
|
result.Host = tenant.GetTenantDomain(_coreSettings);
|
2019-08-08 09:26:58 +00:00
|
|
|
|
2019-11-06 15:03:09 +00:00
|
|
|
#if DEBUG
|
2022-02-15 11:52:43 +00:00
|
|
|
// for Visual Studio debug
|
2022-03-25 16:26:06 +00:00
|
|
|
if (tenant.Alias == LocalHost)
|
2022-02-15 11:52:43 +00:00
|
|
|
{
|
2022-03-25 16:26:06 +00:00
|
|
|
result.Host = LocalHost;
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
2019-11-06 15:03:09 +00:00
|
|
|
#endif
|
2019-08-08 09:26:58 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (!string.IsNullOrEmpty(tenant.MappedDomain))
|
|
|
|
{
|
|
|
|
var mapped = tenant.MappedDomain.ToLowerInvariant();
|
|
|
|
if (!mapped.Contains(Uri.SchemeDelimiter))
|
2019-08-08 09:26:58 +00:00
|
|
|
{
|
2022-06-01 14:07:08 +00:00
|
|
|
mapped = result.Scheme + Uri.SchemeDelimiter + mapped;
|
2019-08-08 09:26:58 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
result = new UriBuilder(mapped);
|
2019-08-08 09:26:58 +00:00
|
|
|
}
|
2019-11-06 15:03:09 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
|
|
|
|
return _serverRootPath = result.Uri.ToString().TrimEnd('/');
|
2019-11-06 15:03:09 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
}
|
2019-11-06 15:03:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public string GetFullAbsolutePath(string virtualPath)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(virtualPath))
|
2019-11-06 15:03:09 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
return ServerRootPath;
|
|
|
|
}
|
2022-02-14 21:02:57 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (virtualPath.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) ||
|
|
|
|
virtualPath.StartsWith("mailto:", StringComparison.InvariantCultureIgnoreCase) ||
|
|
|
|
virtualPath.StartsWith("javascript:", StringComparison.InvariantCultureIgnoreCase) ||
|
|
|
|
virtualPath.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
{
|
|
|
|
return virtualPath;
|
2019-11-06 15:03:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (virtualPath.StartsWith('/'))
|
2019-11-06 15:03:09 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
return ServerRootPath + virtualPath;
|
|
|
|
}
|
2019-11-06 15:03:09 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return ServerRootPath + VirtualRoot.TrimEnd('/') + "/" + virtualPath.TrimStart('~', '/');
|
|
|
|
}
|
2022-02-14 21:02:57 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public string ToAbsolute(string virtualPath)
|
|
|
|
{
|
|
|
|
if (_vpath == null)
|
|
|
|
{
|
|
|
|
return VirtualPathUtility.ToAbsolute(virtualPath);
|
2019-08-19 13:51:36 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (string.IsNullOrEmpty(virtualPath) || virtualPath.StartsWith('/'))
|
2019-08-19 13:51:36 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
return virtualPath;
|
|
|
|
}
|
2019-08-19 13:51:36 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
return (_vpath != "/" ? _vpath : string.Empty) + "/" + virtualPath.TrimStart('~', '/');
|
|
|
|
}
|
2019-08-19 13:51:36 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
public static string GetRegionalUrl(string url, string lang)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(url))
|
|
|
|
{
|
|
|
|
return url;
|
|
|
|
}
|
2019-08-19 13:51:36 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
//-replace language
|
|
|
|
var regex = new Regex("{.*?}");
|
|
|
|
var matches = regex.Matches(url);
|
2020-09-18 07:59:23 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (string.IsNullOrEmpty(lang))
|
|
|
|
{
|
|
|
|
url = matches.Aggregate(url, (current, match) => current.Replace(match.Value, string.Empty));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (Match match in matches)
|
2022-02-14 21:02:57 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
var values = match.Value.TrimStart('{').TrimEnd('}').Split('|');
|
|
|
|
url = url.Replace(match.Value, values.Contains(lang) ? lang : string.Empty);
|
2022-02-14 21:02:57 +00:00
|
|
|
}
|
2019-11-06 15:03:09 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +00:00
|
|
|
//-
|
|
|
|
|
|
|
|
//--remove redundant slashes
|
|
|
|
var uri = new Uri(url);
|
2020-06-03 14:53:58 +00:00
|
|
|
|
2022-02-15 11:52:43 +00:00
|
|
|
if (uri.Scheme == "mailto")
|
2020-06-03 14:53:58 +00:00
|
|
|
{
|
2022-02-15 11:52:43 +00:00
|
|
|
return uri.OriginalString;
|
2020-06-03 14:53:58 +00:00
|
|
|
}
|
2022-02-15 11:52:43 +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('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Initialize(string serverUri, bool localhost = true)
|
|
|
|
{
|
|
|
|
var uri = new Uri(serverUri.Replace('*', 'x').Replace('+', 'x'));
|
2022-03-25 16:26:06 +00:00
|
|
|
_serverRoot = new UriBuilder(uri.Scheme, localhost ? LocalHost : uri.Host, uri.Port);
|
2022-02-15 11:52:43 +00:00
|
|
|
_vpath = "/" + uri.AbsolutePath.Trim('/');
|
2019-10-31 11:28:30 +00:00
|
|
|
}
|
2019-11-06 15:03:09 +00:00
|
|
|
}
|