using Emby.Dlna.Common;
using MediaBrowser.Model.Dlna;
using MediaBrowser.Model.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security;
using System.Text;
namespace Emby.Dlna.Server
{
public class DescriptionXmlBuilder
{
private readonly DeviceProfile _profile;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly string _serverUdn;
private readonly string _serverAddress;
private readonly string _serverName;
private readonly string _serverId;
public DescriptionXmlBuilder(DeviceProfile profile, string serverUdn, string serverAddress, string serverName, string serverId)
{
if (string.IsNullOrEmpty(serverUdn))
{
throw new ArgumentNullException("serverUdn");
}
if (string.IsNullOrEmpty(serverAddress))
{
throw new ArgumentNullException("serverAddress");
}
_profile = profile;
_serverUdn = serverUdn;
_serverAddress = serverAddress;
_serverName = serverName;
_serverId = serverId;
}
private bool EnableAbsoluteUrls
{
get { return false; }
}
public string GetXml()
{
var builder = new StringBuilder();
builder.Append("");
builder.Append("");
builder.Append("");
builder.Append("1");
builder.Append("0");
builder.Append("");
if (!EnableAbsoluteUrls)
{
builder.Append("" + Escape(_serverAddress) + "");
}
AppendDeviceInfo(builder);
builder.Append("");
return builder.ToString();
}
private void AppendDeviceInfo(StringBuilder builder)
{
builder.Append("");
AppendDeviceProperties(builder);
AppendIconList(builder);
builder.Append("" + Escape(_serverAddress) + "/web/index.html");
AppendServiceList(builder);
builder.Append("");
}
private static readonly char[] s_escapeChars = new char[]
{
'<',
'>',
'"',
'\'',
'&'
};
private static readonly string[] s_escapeStringPairs = new string[]
{
"<",
"<",
">",
">",
"\"",
""",
"'",
"'",
"&",
"&"
};
private static string GetEscapeSequence(char c)
{
int num = s_escapeStringPairs.Length;
for (int i = 0; i < num; i += 2)
{
string text = s_escapeStringPairs[i];
string result = s_escapeStringPairs[i + 1];
if (text[0] == c)
{
return result;
}
}
return c.ToString();
}
/// Replaces invalid XML characters in a string with their valid XML equivalent.
/// The input string with invalid characters replaced.
/// The string within which to escape invalid characters.
public static string Escape(string str)
{
if (str == null)
{
return null;
}
StringBuilder stringBuilder = null;
int length = str.Length;
int num = 0;
while (true)
{
int num2 = str.IndexOfAny(s_escapeChars, num);
if (num2 == -1)
{
break;
}
if (stringBuilder == null)
{
stringBuilder = new StringBuilder();
}
stringBuilder.Append(str, num, num2 - num);
stringBuilder.Append(GetEscapeSequence(str[num2]));
num = num2 + 1;
}
if (stringBuilder == null)
{
return str;
}
stringBuilder.Append(str, num, length - num);
return stringBuilder.ToString();
}
private void AppendDeviceProperties(StringBuilder builder)
{
builder.Append("");
builder.Append("DMS-1.50");
builder.Append("M-DMS-1.50");
builder.Append("urn:schemas-upnp-org:device:MediaServer:1");
builder.Append("" + Escape(GetFriendlyName()) + "");
builder.Append("" + Escape(_profile.Manufacturer ?? string.Empty) + "");
builder.Append("" + Escape(_profile.ManufacturerUrl ?? string.Empty) + "");
builder.Append("" + Escape(_profile.ModelDescription ?? string.Empty) + "");
builder.Append("" + Escape(_profile.ModelName ?? string.Empty) + "");
builder.Append("" + Escape(_profile.ModelNumber ?? string.Empty) + "");
builder.Append("" + Escape(_profile.ModelUrl ?? string.Empty) + "");
if (string.IsNullOrEmpty(_profile.SerialNumber))
{
builder.Append("" + Escape(_serverId) + "");
}
else
{
builder.Append("" + Escape(_profile.SerialNumber) + "");
}
builder.Append("");
builder.Append("uuid:" + Escape(_serverUdn) + "");
if (!string.IsNullOrEmpty(_profile.SonyAggregationFlags))
{
builder.Append("" + Escape(_profile.SonyAggregationFlags) + "");
}
}
private string GetFriendlyName()
{
if (string.IsNullOrEmpty(_profile.FriendlyName))
{
return "Jellyfin - " + _serverName;
}
var characterList = new List();
foreach (var c in _serverName)
{
if (char.IsLetterOrDigit(c) || c == '-')
{
characterList.Add(c);
}
}
var characters = characterList.ToArray();
var serverName = new string(characters);
var name = (_profile.FriendlyName ?? string.Empty).Replace("${HostName}", serverName, StringComparison.OrdinalIgnoreCase);
return name;
}
private void AppendIconList(StringBuilder builder)
{
builder.Append("");
foreach (var icon in GetIcons())
{
builder.Append("");
builder.Append("" + Escape(icon.MimeType ?? string.Empty) + "");
builder.Append("" + Escape(icon.Width.ToString(_usCulture)) + "");
builder.Append("" + Escape(icon.Height.ToString(_usCulture)) + "");
builder.Append("" + Escape(icon.Depth ?? string.Empty) + "");
builder.Append("" + BuildUrl(icon.Url) + "");
builder.Append("");
}
builder.Append("");
}
private void AppendServiceList(StringBuilder builder)
{
builder.Append("");
foreach (var service in GetServices())
{
builder.Append("");
builder.Append("" + Escape(service.ServiceType ?? string.Empty) + "");
builder.Append("" + Escape(service.ServiceId ?? string.Empty) + "");
builder.Append("" + BuildUrl(service.ScpdUrl) + "");
builder.Append("" + BuildUrl(service.ControlUrl) + "");
builder.Append("" + BuildUrl(service.EventSubUrl) + "");
builder.Append("");
}
builder.Append("");
}
private string BuildUrl(string url)
{
if (string.IsNullOrEmpty(url))
{
return string.Empty;
}
url = url.TrimStart('/');
url = "/dlna/" + _serverUdn + "/" + url;
if (EnableAbsoluteUrls)
{
url = _serverAddress.TrimEnd('/') + url;
}
return Escape(url);
}
private IEnumerable GetIcons()
{
var list = new List();
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 240,
Height = 240,
Url = "icons/logo240.png"
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 240,
Height = 240,
Url = "icons/logo240.jpg"
});
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 120,
Height = 120,
Url = "icons/logo120.png"
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 120,
Height = 120,
Url = "icons/logo120.jpg"
});
list.Add(new DeviceIcon
{
MimeType = "image/png",
Depth = "24",
Width = 48,
Height = 48,
Url = "icons/logo48.png"
});
list.Add(new DeviceIcon
{
MimeType = "image/jpeg",
Depth = "24",
Width = 48,
Height = 48,
Url = "icons/logo48.jpg"
});
return list;
}
private IEnumerable GetServices()
{
var list = new List();
list.Add(new DeviceService
{
ServiceType = "urn:schemas-upnp-org:service:ContentDirectory:1",
ServiceId = "urn:upnp-org:serviceId:ContentDirectory",
ScpdUrl = "contentdirectory/contentdirectory.xml",
ControlUrl = "contentdirectory/control",
EventSubUrl = "contentdirectory/events"
});
list.Add(new DeviceService
{
ServiceType = "urn:schemas-upnp-org:service:ConnectionManager:1",
ServiceId = "urn:upnp-org:serviceId:ConnectionManager",
ScpdUrl = "connectionmanager/connectionmanager.xml",
ControlUrl = "connectionmanager/control",
EventSubUrl = "connectionmanager/events"
});
if (_profile.EnableMSMediaReceiverRegistrar)
{
list.Add(new DeviceService
{
ServiceType = "urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1",
ServiceId = "urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar",
ScpdUrl = "mediareceiverregistrar/mediareceiverregistrar.xml",
ControlUrl = "mediareceiverregistrar/control",
EventSubUrl = "mediareceiverregistrar/events"
});
}
return list;
}
public override string ToString()
{
return GetXml();
}
}
}