Skip to content

Instantly share code, notes, and snippets.

@dougludlow
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougludlow/1d2ec2a21305cd996613 to your computer and use it in GitHub Desktop.
Save dougludlow/1d2ec2a21305cd996613 to your computer and use it in GitHub Desktop.
Find embedded media (embeds and iframes) in Umbraco that are not using a protocol relative url.
Function Get-EmbeddedCount {
Param([string]$path)
$embedded = @{}
Get-ChildItem $path | ForEach-Object {
[xml]$cache = Get-Content $_.FullName
$regex = "<(?<tag>iframe|embed|param)(?<space>[\S\s-[>]]*?)(?<attr>src|value)=`"(?<protocol>http:)(?<url>//(.)*?)`"(?<therest>[\S\s-[>]]*?)>"
$xpath = "//bodyText"
$nodes = $cache.SelectNodes($xpath) | Where-Object { $_.InnerText -match $regex }
If($nodes.Count) {
$embedded[$_.FullName] = $nodes | ForEach-Object {, $_.ParentNode.Attributes["id"].Value}
}
}
return $embedded
}
Function Get-FormattedEmbeddedCount {
Param([string]$path)
$p1=@{label="Path";Expression={$_.name};}
$p2=@{label="Embed Count";Expression={$_.value.Count};}
$p3=@{label="Nodes";Expression={$_.value -join ", "};}
Get-EmbeddedCount $path | Format-Table $p1,$p2,$p3 -AutoSize
}
Function Update-EmbeddedContent {
Param([string]$path)
$web = New-Object Net.WebClient
$embedded = Get-EmbeddedCount $path
$embedded.Keys | ForEach-Object {
$item = Get-ChildItem $_
$site = $item.Directory.Parent
Copy-Item FixEmbeddedContent.cs (Join-Path $site.FullName "App_Code")
$base = $site.Name
$embedded.Item($_) | ForEach-Object {
$url = "http://$base/base/EmbeddedContent/Fix/$_"
Try {
$response = $web.DownloadString($url)
If ($response -eq "<value>True</value>") {
Write-Host "Success - $url"
}
Else {
Write-Warning "Failure - $url"
}
}
Catch {
Write-Warning "Failure - $url"
}
}
}
}
#Get-FormattedEmbeddedCount c:\inetpub\*\App_Data\umbraco.config
#Update-EmbeddedContent c:\inetpub\*\App_Data\umbraco.config
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using umbraco.presentation.umbracobase;
[RestExtension("EmbeddedContent")]
public class EmbeddedContent
{
[RestExtensionMethod]
public static string Fix(int nodeId)
{
bool @fixed = false;
var document = new Document(nodeId);
var bodyText = document.getProperty("bodyText");
if (bodyText != null)
{
string regex = "<(?<tag>iframe|embed|param)(?<space>[\\S\\s-[>]]*?)(?<attr>src|value)=\"(?<protocol>http:)(?<url>//(.)*?)\"(?<therest>[\\S\\s-[>]]*?)>";
if (Regex.IsMatch(bodyText.Value.ToString(), regex))
{
bodyText.Value = Regex.Replace(bodyText.Value.ToString(), regex, "<${tag}${space}${attr}=\"${url}\"${therest}>");
document.Publish(User.GetUser(0));
umbraco.library.UpdateDocumentCache(document.Id);
umbraco.library.RefreshContent();
@fixed = true;
}
}
return @fixed.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment