2009-06-19 07:39:16 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Web;
|
2012-08-20 11:56:16 -01:00
|
|
|
using Umbraco.Core.IO;
|
2012-12-10 05:03:28 +05:00
|
|
|
using Umbraco.Core.Logging;
|
2009-06-19 07:39:16 +00:00
|
|
|
using umbraco.BusinessLogic;
|
2012-07-04 09:15:16 -02:00
|
|
|
using umbraco.cms.businesslogic.Files;
|
2010-01-28 12:51:46 +00:00
|
|
|
using umbraco.IO;
|
2009-06-19 07:39:16 +00:00
|
|
|
|
|
|
|
|
namespace umbraco.editorControls.tinymce
|
|
|
|
|
{
|
|
|
|
|
internal class tinyMCEImageHelper
|
|
|
|
|
{
|
|
|
|
|
public static string cleanImages(string html)
|
|
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
var allowedAttributes = UmbracoSettings.ImageAllowedAttributes.ToLower().Split(',');
|
|
|
|
|
const string pattern = @"<img [^>]*>";
|
|
|
|
|
var tags = Regex.Matches(html + " ", pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
|
2009-06-19 07:39:16 +00:00
|
|
|
foreach (Match tag in tags)
|
|
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
if (tag.Value.ToLower().IndexOf("umbraco_macro", StringComparison.Ordinal) == -1)
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
var cleanTag = "<img";
|
2009-06-19 07:39:16 +00:00
|
|
|
// gather all attributes
|
|
|
|
|
// TODO: This should be replaced with a general helper method - but for now we'll wanna leave umbraco.dll alone for this patch
|
2012-12-26 06:27:30 -01:00
|
|
|
var ht = new Hashtable();
|
|
|
|
|
var matches = Regex.Matches(tag.Value.Replace(">", " >"), "(?<attributeName>\\S*)=\"(?<attributeValue>[^\"]*)\"|(?<attributeName>\\S*)=(?<attributeValue>[^\"|\\s]*)\\s", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
|
|
|
|
|
foreach (Match attributeSet in matches)
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
ht.Add(attributeSet.Groups["attributeName"].Value.ToLower(),
|
|
|
|
|
attributeSet.Groups["attributeValue"].Value);
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If rel attribute exists and if it's different from current sizing we should resize the image!
|
|
|
|
|
if (helper.FindAttribute(ht, "rel") != "")
|
|
|
|
|
{
|
|
|
|
|
// if size has changed resize image serverside
|
2012-12-26 06:27:30 -01:00
|
|
|
var newDims = helper.FindAttribute(ht, "rel").Split(",".ToCharArray());
|
|
|
|
|
if (newDims.Length > 0 && (newDims[0] != helper.FindAttribute(ht, "width") || newDims[1] != helper.FindAttribute(ht, "height")))
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
int newWidth;
|
|
|
|
|
int newHeight;
|
|
|
|
|
cleanTag += DoResize(ht, out newWidth, out newHeight);
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception err)
|
|
|
|
|
{
|
|
|
|
|
cleanTag += " src=\"" + helper.FindAttribute(ht, "src") + "\"";
|
|
|
|
|
if (helper.FindAttribute(ht, "width") != "")
|
|
|
|
|
{
|
|
|
|
|
cleanTag += " width=\"" + helper.FindAttribute(ht, "width") + "\"";
|
|
|
|
|
cleanTag += " height=\"" + helper.FindAttribute(ht, "height") + "\"";
|
|
|
|
|
}
|
2012-12-10 05:03:28 +05:00
|
|
|
LogHelper.Error<tinyMCEImageHelper>("Error resizing image in editor", err);
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
2011-08-11 13:03:27 -12:00
|
|
|
}
|
|
|
|
|
else
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
|
|
|
|
if (helper.FindAttribute(ht, "width") != "")
|
|
|
|
|
{
|
|
|
|
|
cleanTag += " width=\"" + helper.FindAttribute(ht, "width") + "\"";
|
|
|
|
|
cleanTag += " height=\"" + helper.FindAttribute(ht, "height") + "\"";
|
|
|
|
|
}
|
2011-08-11 13:03:27 -12:00
|
|
|
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (helper.FindAttribute(ht, "width") != "")
|
|
|
|
|
{
|
|
|
|
|
cleanTag += " width=\"" + helper.FindAttribute(ht, "width") + "\"";
|
|
|
|
|
cleanTag += " height=\"" + helper.FindAttribute(ht, "height") + "\"";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build image tag
|
2012-12-26 06:27:30 -01:00
|
|
|
foreach (var attr in allowedAttributes)
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
|
|
|
|
if (helper.FindAttribute(ht, attr) != "")
|
|
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
var attrValue = helper.FindAttribute(ht, attr);
|
2009-06-19 07:39:16 +00:00
|
|
|
cleanTag += " " + attr + "=\"" + attrValue + "\"";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bool.Parse(GlobalSettings.EditXhtmlMode))
|
|
|
|
|
cleanTag += "/";
|
2012-12-26 06:27:30 -01:00
|
|
|
|
2009-06-19 07:39:16 +00:00
|
|
|
cleanTag += ">";
|
|
|
|
|
html = html.Replace(tag.Value, cleanTag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return html;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-26 06:27:30 -01:00
|
|
|
private static string DoResize(IDictionary attributes, out int finalWidth, out int finalHeight)
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
2012-11-07 09:30:40 +05:00
|
|
|
var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
|
2012-12-26 06:27:30 -01:00
|
|
|
var orgSrc = HttpContext.Current.Server.HtmlDecode(helper.FindAttribute(attributes, "src").Replace("%20", " "));
|
|
|
|
|
var orgDim = helper.FindAttribute(attributes, "rel").Split(",".ToCharArray());
|
|
|
|
|
var orgWidth = float.Parse(orgDim[0]);
|
|
|
|
|
var orgHeight = float.Parse(orgDim[1]);
|
|
|
|
|
var newWidth = int.Parse(helper.FindAttribute(attributes, "width"));
|
|
|
|
|
var newHeight = int.Parse(helper.FindAttribute(attributes, "height"));
|
2012-08-20 11:56:16 -01:00
|
|
|
|
2012-12-26 06:27:30 -01:00
|
|
|
var newSrc = "";
|
2009-06-19 07:39:16 +00:00
|
|
|
|
2012-12-26 06:27:30 -01:00
|
|
|
if (orgHeight > 0 && orgWidth > 0 && orgSrc != "")
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
|
|
|
|
// Check dimensions
|
2011-08-11 13:03:27 -12:00
|
|
|
if (Math.Abs(orgWidth / newWidth) > Math.Abs(orgHeight / newHeight))
|
2009-06-19 07:39:16 +00:00
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
newHeight = (int)Math.Round(newWidth * (orgHeight / orgWidth));
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-12-26 06:27:30 -01:00
|
|
|
newWidth = (int)Math.Round(newHeight * (orgWidth / orgHeight));
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
2012-08-20 11:56:16 -01:00
|
|
|
var orgPath = fs.GetRelativePath(orgSrc);
|
|
|
|
|
if (fs.FileExists(orgPath))
|
|
|
|
|
{
|
|
|
|
|
var uf = new UmbracoFile(orgPath);
|
|
|
|
|
newSrc = uf.Resize(newWidth, newHeight);
|
|
|
|
|
}
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finalWidth = newWidth;
|
|
|
|
|
finalHeight = newHeight;
|
|
|
|
|
|
2012-12-26 06:27:30 -01:00
|
|
|
return " src=\"" + newSrc + "\" width=\"" + newWidth + "\" height=\"" + newHeight + "\"";
|
2009-06-19 07:39:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|