using System; using System.Collections; using System.Text.RegularExpressions; using System.Web; using Umbraco.Core.IO; using Umbraco.Core.Logging; using umbraco.cms.businesslogic.Files; namespace umbraco.editorControls.tinymce { internal class tinyMCEImageHelper { public static string cleanImages(string html) { var allowedAttributes = UmbracoSettings.ImageAllowedAttributes.ToLower().Split(','); const string pattern = @"]*>"; var tags = Regex.Matches(html + " ", pattern, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (Match tag in tags) { if (tag.Value.ToLower().IndexOf("umbraco_macro", StringComparison.Ordinal) == -1) { var cleanTag = "", " >"), "(?\\S*)=\"(?[^\"]*)\"|(?\\S*)=(?[^\"|\\s]*)\\s", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (Match attributeSet in matches) { ht.Add(attributeSet.Groups["attributeName"].Value.ToLower(), attributeSet.Groups["attributeValue"].Value); } // 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 var newDims = helper.FindAttribute(ht, "rel").Split(",".ToCharArray()); if (newDims.Length > 0 && (newDims[0] != helper.FindAttribute(ht, "width") || newDims[1] != helper.FindAttribute(ht, "height"))) { try { int newWidth; int newHeight; cleanTag += DoResize(ht, out newWidth, out newHeight); } 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") + "\""; } LogHelper.Error("Error resizing image in editor", err); } } else { if (helper.FindAttribute(ht, "width") != "") { cleanTag += " width=\"" + helper.FindAttribute(ht, "width") + "\""; cleanTag += " height=\"" + helper.FindAttribute(ht, "height") + "\""; } } } else { if (helper.FindAttribute(ht, "width") != "") { cleanTag += " width=\"" + helper.FindAttribute(ht, "width") + "\""; cleanTag += " height=\"" + helper.FindAttribute(ht, "height") + "\""; } } // Build image tag foreach (var attr in allowedAttributes) { if (helper.FindAttribute(ht, attr) != "") { var attrValue = helper.FindAttribute(ht, attr); cleanTag += " " + attr + "=\"" + attrValue + "\""; } } if (bool.Parse(GlobalSettings.EditXhtmlMode)) cleanTag += "/"; cleanTag += ">"; html = html.Replace(tag.Value, cleanTag); } } return html; } private static string DoResize(IDictionary attributes, out int finalWidth, out int finalHeight) { var fs = FileSystemProviderManager.Current.GetFileSystemProvider(); 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")); var newSrc = ""; if (orgHeight > 0 && orgWidth > 0 && orgSrc != "") { // Check dimensions if (Math.Abs(orgWidth / newWidth) > Math.Abs(orgHeight / newHeight)) { newHeight = (int)Math.Round(newWidth * (orgHeight / orgWidth)); } else { newWidth = (int)Math.Round(newHeight * (orgWidth / orgHeight)); } var orgPath = fs.GetRelativePath(orgSrc); if (fs.FileExists(orgPath)) { var uf = new UmbracoFile(orgPath); newSrc = uf.Resize(newWidth, newHeight); } } finalWidth = newWidth; finalHeight = newHeight; return " src=\"" + newSrc + "\" width=\"" + newWidth + "\" height=\"" + newHeight + "\""; } } }