Skip to content

Commit

Permalink
Fix issue #67791: [WFFM] Attached file in e-mail
Browse files Browse the repository at this point in the history
Fix details: Replace the "AddAttachments" processor and expand it by the SetCorrectValueForTheField method to include the posted file in the email attachments and place a path of the appropriate item from CM to the email body.
  • Loading branch information
Pavel committed Dec 12, 2016
1 parent a1705b6 commit 9e081dc
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Sitecore.Support.67791.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9E4D5CB8-0FC1-4788-8A8F-D79B931B2D86}") = "Sitecore.Support.67791", "Sitecore.Support.67791\Sitecore.Support.67791.csproj", "{20B901DE-BB11-4A2A-A000-31493380B28D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{20B901DE-BB11-4A2A-A000-31493380B28D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{20B901DE-BB11-4A2A-A000-31493380B28D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20B901DE-BB11-4A2A-A000-31493380B28D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20B901DE-BB11-4A2A-A000-31493380B28D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
<sitecore>
<pipelines>
<processMessage>
<processor method="AddAttachments" patch:after="processor[1]">
<patch:attribute name="type">Sitecore.Support.Forms.Core.Pipelines.ProcessMessage, Sitecore.Support.67791</patch:attribute>
</processor>
</processMessage>
</pipelines>
</sitecore>
</configuration>
125 changes: 125 additions & 0 deletions src/Sitecore.Support.67791/Forms/Core/Pipelines/ProcessMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Extensions.StringExtensions;
using Sitecore.Form.Core.Configuration;
using Sitecore.Form.Core.Utility;
using Sitecore.Forms.Core.Data;
using Sitecore.WFFM.Abstractions.Actions;
using Sitecore.WFFM.Abstractions.Dependencies;
using Sitecore.WFFM.Abstractions.Mail;
using Sitecore.WFFM.Abstractions.Shared;
using Sitecore.WFFM.Abstractions.Utils;

namespace Sitecore.Support.Forms.Core.Pipelines
{
public class ProcessMessage
{
private readonly string hrefReplacer;
private readonly string shortHrefMediaReplacer;
private readonly string shortHrefReplacer;
private readonly string srcReplacer;

public ProcessMessage() : this(DependenciesManager.WebUtil) {}

public ProcessMessage(IWebUtil webUtil)
{
Assert.IsNotNull(webUtil, "webUtil");
srcReplacer = string.Join(string.Empty, "src=\"", webUtil.GetServerUrl(), "/~");
shortHrefReplacer = string.Join(string.Empty, "href=\"", webUtil.GetServerUrl(), "/");
shortHrefMediaReplacer = string.Join(string.Empty, "href=\"", webUtil.GetServerUrl(), "/~/");
hrefReplacer = shortHrefReplacer + "~";
}

public IFieldProvider FieldProvider { get; set; }

public IItemRepository ItemRepository { get; set; }

public void AddAttachments(ProcessMessageArgs args)
{
Assert.IsNotNull(ItemRepository, "ItemRepository");
foreach (AdaptedControlResult result in args.Fields)
{
if (string.IsNullOrEmpty(result.Parameters) || !result.Parameters.StartsWith("medialink") || string.IsNullOrEmpty(result.Value)) continue;

var uri = ItemUri.Parse(result.Value);

if (uri != null)
{
var innerItem = ItemRepository.GetItem(uri);
if (innerItem == null) continue;

var item2 = new MediaItem(innerItem);
args.Attachments.Add(new Attachment(item2.GetMediaStream(), string.Join(".", item2.Name, item2.Extension), item2.MimeType));
}
else
{
var fileFromRequest = GetFileFromRequest(result.FieldID);

if (fileFromRequest != null)
{
args.Attachments.Add(new Attachment(fileFromRequest.InputStream, fileFromRequest.FileName,
fileFromRequest.ContentType));
}

SetCorrectValueForTheField(result, fileFromRequest);
}
}
}

private HttpPostedFile GetFileFromRequest(string fieldId)
{
if ((HttpContext.Current == null) || (HttpContext.Current.Request.Files.Count == 0)) return null;

var files = HttpContext.Current.Request.Files;
var allKeys = files.AllKeys;

if (HttpContext.Current.Handler.GetType() == typeof(MvcHandler))
{
var keys = HttpContext.Current.Request.Form.AllKeys;
for (var i = 0; i < allKeys.Length; i++)
{
if (!keys.Any(key => key.Contains(fieldId) && allKeys[i].Contains(key.Replace(fieldId, "")))) continue;

var file = files[i];
file.InputStream.Position = 0L;

return file;
}
}
else
{
var str = fieldId.Replace("-", string.Empty).TrimStart('{').TrimEnd('}');
for (var k = 0; k < allKeys.Length; k++)
{
if (allKeys[k].Contains(str)) return files[k];
}
}
return null;
}

private void SetCorrectValueForTheField(AdaptedControlResult field, HttpPostedFile postedFile)
{
string str2;
var item = new FieldItem(StaticSettings.ContextDatabase.GetItem(field.FieldID));
var text = item["Parameters"];
var index = text.IndexOf("<UploadTo>", StringComparison.InvariantCultureIgnoreCase);
var length = text.IndexOf("</UploadTo>", StringComparison.InvariantCultureIgnoreCase) - index;

if ((index < length) && (length > 0))
{
str2 = text.Mid(index, length);
}
else
{
str2 = "/sitecore/media library/";
}
ReflectionUtils.SetProperty(field, "Value", string.Format("{0}{1} (attached)", str2, postedFile.FileName), true);
}
}
}
6 changes: 6 additions & 0 deletions src/Sitecore.Support.67791/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Sitecore.Support.67791")]
[assembly: AssemblyProduct("Sitecore.Support.67791")]
[assembly: ComVisible(false)]
129 changes: 129 additions & 0 deletions src/Sitecore.Support.67791/Sitecore.Support.67791.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{20B901DE-BB11-4A2A-A000-31493380B28D}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Sitecore.Support</RootNamespace>
<AssemblyName>Sitecore.Support.67791</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<UseIISExpress>true</UseIISExpress>
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Sitecore.Forms.Core, Version=8.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\WFFM.Sitecore.Forms.Core.8.1.2\lib\Sitecore.Forms.Core.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.Kernel">
<HintPath>..\packages\SC.Sitecore.Kernel.8.1.2\lib\Sitecore.Kernel.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Sitecore.WFFM.Abstractions, Version=8.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\WFFM.Sitecore.WFFM.Abstractions.8.1.2\lib\Sitecore.WFFM.Abstractions.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.WebPages.Deployment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup>
<Compile Include="Forms\Core\Pipelines\ProcessMessage.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="App_Config\Include\zzz\Sitecore.Support.67791.config" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Content Include="Views\Form\EditorTemplates\FileUploadField.cshtml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="web.config" />
</ItemGroup>
<ItemGroup />
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>0</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:55626/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@using Sitecore.Forms.Mvc.Html
@model Sitecore.Forms.Mvc.ViewModels.Fields.FileUploadField

@using (Html.BeginField())
{
@Html.TextBoxFor(m => m.Value, new { type = "file" })
@Html.Hidden(Model.FieldItemId, Model.FieldItemId)
}
11 changes: 11 additions & 0 deletions src/Sitecore.Support.67791/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="SC.Sitecore.Kernel" version="8.1.2" targetFramework="net45" />
<package id="SC.System.Web.Mvc" version="8.1.2" targetFramework="net45" />
<package id="WFFM.Sitecore.Forms.Core" version="8.1.2" targetFramework="net45" />
<package id="WFFM.Sitecore.WFFM.Abstractions" version="8.1.2" targetFramework="net45" />
</packages>
20 changes: 20 additions & 0 deletions src/Sitecore.Support.67791/web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!-- Do not change this file - it must be empty, see https://github.com/SitecoreSupport/PatchCreator/issues/13 -->
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

0 comments on commit 9e081dc

Please sign in to comment.