DocSpace-client/common/ASC.Notify.Textile/TextileStyler.cs

310 lines
13 KiB
C#
Raw Normal View History

2019-08-19 13:51:36 +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.
*
*/
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ASC.Common.Notify.Patterns;
using ASC.Core;
using ASC.Core.Common.WhiteLabel;
using ASC.Notify.Messages;
using ASC.Notify.Patterns;
using ASC.Notify.Textile.Resources;
2019-10-09 15:04:46 +00:00
using ASC.Security.Cryptography;
2019-08-19 13:51:36 +00:00
using ASC.Web.Core.WhiteLabel;
using Microsoft.AspNetCore.WebUtilities;
2019-09-27 15:12:13 +00:00
using Microsoft.Extensions.Configuration;
2019-08-19 13:51:36 +00:00
using Textile;
using Textile.Blocks;
namespace ASC.Notify.Textile
{
public class TextileStyler : IPatternStyler
{
private static readonly Regex VelocityArguments = new Regex(NVelocityPatternFormatter.NoStylePreffix + "(?<arg>.*?)" + NVelocityPatternFormatter.NoStyleSuffix, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
2019-09-20 12:20:03 +00:00
public CoreBaseSettings CoreBaseSettings { get; }
2019-09-27 15:12:13 +00:00
public IConfiguration Configuration { get; }
2019-10-09 15:04:46 +00:00
public InstanceCrypto InstanceCrypto { get; }
2019-09-20 12:20:03 +00:00
2019-08-19 13:51:36 +00:00
static TextileStyler()
{
const string file = "ASC.Notify.Textile.Resources.style.css";
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(file);
using var reader = new StreamReader(stream);
BlockAttributesParser.Styler = new StyleReader(reader.ReadToEnd().Replace("\n", "").Replace("\r", ""));
}
2019-10-09 15:04:46 +00:00
public TextileStyler(CoreBaseSettings coreBaseSettings, IConfiguration configuration, InstanceCrypto instanceCrypto)
2019-09-20 12:20:03 +00:00
{
CoreBaseSettings = coreBaseSettings;
2019-09-27 15:12:13 +00:00
Configuration = configuration;
2019-10-09 15:04:46 +00:00
InstanceCrypto = instanceCrypto;
2019-09-20 12:20:03 +00:00
}
2019-08-19 13:51:36 +00:00
public void ApplyFormating(NoticeMessage message)
{
var output = new StringBuilderTextileFormatter();
var formatter = new TextileFormatter(output);
if (!string.IsNullOrEmpty(message.Subject))
{
message.Subject = VelocityArguments.Replace(message.Subject, m => m.Result("${arg}"));
}
if (string.IsNullOrEmpty(message.Body)) return;
formatter.Format(message.Body);
var template = GetTemplate(message);
var analytics = GetAnalytics(message);
var logoImg = GetLogoImg(message);
var logoText = GetLogoText(message);
var mailSettings = GetMailSettings(message);
var unsubscribeText = GetUnsubscribeText(message, mailSettings);
InitFooter(message, mailSettings, out var footerContent, out var footerSocialContent);
message.Body = template.Replace("%ANALYTICS%", analytics)
.Replace("%CONTENT%", output.GetFormattedText())
.Replace("%LOGO%", logoImg)
.Replace("%LOGOTEXT%", logoText)
2019-09-23 12:20:08 +00:00
.Replace("%SITEURL%", mailSettings == null ? mailSettings.DefaultMailSiteUrl : mailSettings.SiteUrl)
2019-08-19 13:51:36 +00:00
.Replace("%FOOTER%", footerContent)
.Replace("%FOOTERSOCIAL%", footerSocialContent)
.Replace("%TEXTFOOTER%", unsubscribeText);
}
private static string GetTemplate(NoticeMessage message)
{
var template = NotifyTemplateResource.HtmlMaster;
var templateTag = message.GetArgument("MasterTemplate");
if (templateTag != null)
{
var templateTagValue = templateTag.Value as string;
if (!string.IsNullOrEmpty(templateTagValue))
{
var templateValue = NotifyTemplateResource.ResourceManager.GetString(templateTagValue);
if (!string.IsNullOrEmpty(templateValue))
template = templateValue;
}
}
return template;
}
private static string GetAnalytics(NoticeMessage message)
{
var analyticsTag = message.GetArgument("Analytics");
return analyticsTag == null ? string.Empty : (string)analyticsTag.Value;
}
2019-09-20 12:20:03 +00:00
private string GetLogoImg(NoticeMessage message)
2019-08-19 13:51:36 +00:00
{
string logoImg;
2019-09-20 12:20:03 +00:00
if (CoreBaseSettings.Personal && !CoreBaseSettings.CustomMode)
2019-08-19 13:51:36 +00:00
{
logoImg = "https://static.onlyoffice.com/media/newsletters/images-v10/mail_logo.png";
}
else
{
2019-09-27 15:12:13 +00:00
logoImg = Configuration["web:logo:mail"];
2019-08-19 13:51:36 +00:00
if (string.IsNullOrEmpty(logoImg))
{
var logo = message.GetArgument("LetterLogo");
if (logo != null && (string)logo.Value != "")
{
logoImg = (string)logo.Value;
}
else
{
logoImg = "https://static.onlyoffice.com/media/newsletters/images-v10/mail_logo.png";
}
}
}
return logoImg;
}
2019-09-27 15:12:13 +00:00
private string GetLogoText(NoticeMessage message)
2019-08-19 13:51:36 +00:00
{
2019-09-27 15:12:13 +00:00
var logoText = Configuration["web:logotext:mail"];
2019-08-19 13:51:36 +00:00
if (string.IsNullOrEmpty(logoText))
{
var llt = message.GetArgument("LetterLogoText");
if (llt != null && (string)llt.Value != "")
{
logoText = (string)llt.Value;
}
else
{
logoText = BaseWhiteLabelSettings.DefaultLogoText;
}
}
return logoText;
}
private static MailWhiteLabelSettings GetMailSettings(NoticeMessage message)
{
var mailWhiteLabelTag = message.GetArgument("MailWhiteLabelSettings");
return mailWhiteLabelTag == null ? null : mailWhiteLabelTag.Value as MailWhiteLabelSettings;
}
private static void InitFooter(NoticeMessage message, MailWhiteLabelSettings settings, out string footerContent, out string footerSocialContent)
{
footerContent = string.Empty;
footerSocialContent = string.Empty;
var footer = message.GetArgument("Footer");
if (footer == null) return;
var footerValue = (string)footer.Value;
if (string.IsNullOrEmpty(footerValue)) return;
switch (footerValue)
{
case "common":
InitCommonFooter(settings, out footerContent, out footerSocialContent);
break;
case "social":
InitSocialFooter(settings, out footerSocialContent);
break;
case "personal":
footerSocialContent = NotifyTemplateResource.SocialNetworksFooter;
break;
case "personalCustomMode":
break;
case "opensource":
footerContent = NotifyTemplateResource.FooterOpensource;
footerSocialContent = NotifyTemplateResource.SocialNetworksFooter;
break;
}
}
private static void InitCommonFooter(MailWhiteLabelSettings settings, out string footerContent, out string footerSocialContent)
{
footerContent = string.Empty;
footerSocialContent = string.Empty;
if (settings == null)
{
footerContent =
NotifyTemplateResource.FooterCommon
2019-09-23 12:20:08 +00:00
.Replace("%SUPPORTURL%", settings.DefaultMailSupportUrl)
.Replace("%SALESEMAIL%", settings.DefaultMailSalesEmail)
.Replace("%DEMOURL%", settings.DefaultMailDemotUrl);
2019-08-19 13:51:36 +00:00
footerSocialContent = NotifyTemplateResource.SocialNetworksFooter;
}
else if (settings.FooterEnabled)
{
footerContent =
NotifyTemplateResource.FooterCommon
.Replace("%SUPPORTURL%", string.IsNullOrEmpty(settings.SupportUrl) ? "mailto:" + settings.SalesEmail : settings.SupportUrl)
.Replace("%SALESEMAIL%", settings.SalesEmail)
.Replace("%DEMOURL%", string.IsNullOrEmpty(settings.DemotUrl) ? "mailto:" + settings.SalesEmail : settings.DemotUrl);
footerSocialContent = settings.FooterSocialEnabled ? NotifyTemplateResource.SocialNetworksFooter : string.Empty;
}
}
private static void InitSocialFooter(MailWhiteLabelSettings settings, out string footerSocialContent)
{
footerSocialContent = string.Empty;
if (settings == null || (settings.FooterEnabled && settings.FooterSocialEnabled))
footerSocialContent = NotifyTemplateResource.SocialNetworksFooter;
}
2019-09-20 12:20:03 +00:00
private string GetUnsubscribeText(NoticeMessage message, MailWhiteLabelSettings settings)
2019-08-19 13:51:36 +00:00
{
var withoutUnsubscribe = message.GetArgument("WithoutUnsubscribe");
if (withoutUnsubscribe != null && (bool)withoutUnsubscribe.Value)
return string.Empty;
var rootPathArgument = message.GetArgument("__VirtualRootPath");
var rootPath = rootPathArgument == null ? string.Empty : (string)rootPathArgument.Value;
if (string.IsNullOrEmpty(rootPath))
return string.Empty;
2019-09-20 12:20:03 +00:00
var unsubscribeLink = CoreBaseSettings.CustomMode && CoreBaseSettings.Personal
2019-08-19 13:51:36 +00:00
? GetSiteUnsubscribeLink(message, settings)
: GetPortalUnsubscribeLink(message, settings);
if (string.IsNullOrEmpty(unsubscribeLink))
return string.Empty;
return string.Format(NotifyTemplateResource.TextForFooterWithUnsubscribeLink, rootPath, unsubscribeLink);
}
2019-09-20 12:20:03 +00:00
private string GetPortalUnsubscribeLink(NoticeMessage message, MailWhiteLabelSettings settings)
2019-08-19 13:51:36 +00:00
{
var unsubscribeLinkArgument = message.GetArgument("ProfileUrl");
if (unsubscribeLinkArgument != null)
{
var unsubscribeLink = (string)unsubscribeLinkArgument.Value;
if (!string.IsNullOrEmpty(unsubscribeLink))
return unsubscribeLink;
}
return GetSiteUnsubscribeLink(message, settings);
}
2019-09-20 12:20:03 +00:00
private string GetSiteUnsubscribeLink(NoticeMessage message, MailWhiteLabelSettings settings)
2019-08-19 13:51:36 +00:00
{
var mail = message.Recipient.Addresses.FirstOrDefault(r => r.Contains("@"));
if (string.IsNullOrEmpty(mail))
return string.Empty;
2019-09-20 12:20:03 +00:00
var format = CoreBaseSettings.CustomMode
2019-08-19 13:51:36 +00:00
? "{0}/unsubscribe/{1}"
: "{0}/Unsubscribe.aspx?id={1}";
var site = settings == null
2019-09-23 12:20:08 +00:00
? settings.DefaultMailSiteUrl
2019-08-19 13:51:36 +00:00
: settings.SiteUrl;
return string.Format(format,
site,
WebEncoders.Base64UrlEncode(
2019-10-09 15:04:46 +00:00
InstanceCrypto.Encrypt(
2019-08-19 13:51:36 +00:00
Encoding.UTF8.GetBytes(mail.ToLowerInvariant()))));
}
}
}