using System;
using CMS.CMSHelper;
using CMS.GlobalHelper;
using CMS.PortalControls;
using CMS.PortalEngine;
using CMS.SettingsProvider;
public partial class CMSWebParts_General_MobileDeviceRedirection : CMSAbstractWebPart
{
#region "Constants"
private const string SETTINGS_DEVICE_AUTOMATIC = "automatic";
private const string SETTINGS_DEVICE_SMALL = "small";
private const string SETTINGS_DEVICE_LARGE = "large";
#endregion
#region "Properties"
///
/// Small device redirection URL.
///
public string SmallDeviceRedirectionURL
{
get
{
return ValidationHelper.GetString(GetValue("SmallDeviceRedirectionURL"), string.Empty);
}
set
{
SetValue("SmallDeviceRedirectionURL", value);
}
}
///
/// Large device redirection URL.
///
public string LargeDeviceRedirectionURL
{
get
{
return ValidationHelper.GetString(GetValue("LargeDeviceRedirectionURL"), string.Empty);
}
set
{
SetValue("LargeDeviceRedirectionURL", value);
}
}
///
/// Redirect iPhone.
///
public string RedirectIPhone
{
get
{
return ValidationHelper.GetString(GetValue("RedirectIPhone"), string.Empty);
}
set
{
SetValue("RedirectIPhone", value);
}
}
///
/// Redirect Nokia.
///
public string RedirectNokia
{
get
{
return ValidationHelper.GetString(GetValue("RedirectNokia"), string.Empty);
}
set
{
SetValue("RedirectNokia", value);
}
}
///
/// Redirect BlackBerry.
///
public string RedirectBlackBerry
{
get
{
return ValidationHelper.GetString(GetValue("RedirectBlackBerry"), string.Empty);
}
set
{
SetValue("RedirectBlackBerry", value);
}
}
///
/// Redirect iPad.
///
public string RedirectIPad
{
get
{
return ValidationHelper.GetString(GetValue("RedirectIPad"), string.Empty);
}
set
{
SetValue("RedirectIPad", value);
}
}
///
/// Redirect Android.
///
public string RedirectAndroid
{
get
{
return ValidationHelper.GetString(GetValue("RedirectAndroid"), string.Empty);
}
set
{
SetValue("RedirectAndroid", value);
}
}
///
/// Always redirect.
///
public bool AlwaysRedirect
{
get
{
return ValidationHelper.GetBoolean(GetValue("AlwaysRedirect"), false);
}
set
{
SetValue("AlwaysRedirect", value);
}
}
///
/// Other small devices (User agent).
///
public string OtherSmallDevices
{
get
{
return ValidationHelper.GetString(GetValue("OtherSmallDevices"), string.Empty);
}
set
{
SetValue("OtherSmallDevices", value);
}
}
///
/// Other large devices (User agent).
///
public string OtherLargeDevices
{
get
{
return ValidationHelper.GetString(GetValue("OtherLargeDevices"), string.Empty);
}
set
{
SetValue("OtherLargeDevices", value);
}
}
#endregion
#region "Webpart methods"
///
/// Content loaded event handler.
///
public override void OnContentLoaded()
{
base.OnContentLoaded();
SetupControl();
}
///
/// Initializes the control properties.
///
protected void SetupControl()
{
if (!StopProcessing)
{
// Trim blank characters
string rawSmallDeviceURL = SmallDeviceRedirectionURL.Trim();
string rawLargeDeviceURL = LargeDeviceRedirectionURL.Trim();
// Get redirect cookie
string redirected = CookieHelper.GetValue(CookieName.MobileRedirected);
if ((AlwaysRedirect || String.IsNullOrEmpty(redirected)) && (CMSContext.ViewMode == ViewModeEnum.LiveSite))
{
string redirectUrl = null;
// Get user agent name or user agent string
string userAgent = BrowserHelper.GetUserAgent();
if (!string.IsNullOrEmpty(userAgent))
{
UserAgentEnum userAgentEnum = GetUserAgentName(userAgent);
string redirectSettings = null;
switch (userAgentEnum)
{
case UserAgentEnum.BlackBerry:
redirectSettings = RedirectBlackBerry;
break;
case UserAgentEnum.IPhone:
redirectSettings = RedirectIPhone;
break;
case UserAgentEnum.IPad:
redirectSettings = RedirectIPad;
break;
case UserAgentEnum.Nokia:
redirectSettings = RedirectNokia;
break;
case UserAgentEnum.Android:
redirectSettings = RedirectAndroid;
break;
}
if (redirectSettings != null)
{
redirectUrl = GetRedirectUrl(redirectSettings, userAgent, userAgentEnum, rawSmallDeviceURL, rawLargeDeviceURL);
}
else
{
redirectUrl = CheckUsersRedirection(userAgent, rawSmallDeviceURL, rawLargeDeviceURL);
}
}
// Check if some address is specified
if (!string.IsNullOrEmpty(redirectUrl))
{
string newURL = URLHelper.ResolveUrl(redirectUrl);
// If current URL is same as set, no redirection is done
if ((URLHelper.CurrentURL != newURL) && (URLHelper.GetAbsoluteUrl(URLHelper.CurrentURL) != newURL))
{
// Set redirected cookie
CookieHelper.SetValue(CookieName.MobileRedirected, "true", DateTimeHelper.ZERO_TIME);
URLHelper.ResponseRedirect(newURL);
}
}
}
}
}
///
/// Returns redirect URL for specified user agent and redirection settings.
///
protected string GetRedirectUrl(string redirectSettings, string userAgent, UserAgentEnum userAgentEnum, string smallUrl, string largeUrl)
{
switch (redirectSettings)
{
case SETTINGS_DEVICE_AUTOMATIC:
string url = CheckUsersRedirection(userAgent, smallUrl, largeUrl);
if (!string.IsNullOrEmpty(url))
{
return url;
}
else
{
switch (userAgentEnum)
{
case UserAgentEnum.BlackBerry:
return smallUrl;
case UserAgentEnum.IPhone:
return largeUrl;
case UserAgentEnum.IPad:
return largeUrl;
case UserAgentEnum.Nokia:
return largeUrl;
case UserAgentEnum.Android:
return largeUrl;
default:
return smallUrl;
}
}
case SETTINGS_DEVICE_SMALL:
return smallUrl;
case SETTINGS_DEVICE_LARGE:
return largeUrl;
default:
return string.Empty;
}
}
///
/// Returns user agent enum for given user agent string.
///
protected UserAgentEnum GetUserAgentName(string userAgent)
{
userAgent = userAgent.ToLowerCSafe();
if (userAgent.Contains(UserAgentEnum.BlackBerry.ToString().ToLowerCSafe()))
{
return UserAgentEnum.BlackBerry;
}
else if (userAgent.Contains(UserAgentEnum.IPhone.ToString().ToLowerCSafe()))
{
return UserAgentEnum.IPhone;
}
else if (userAgent.Contains(UserAgentEnum.IPad.ToString().ToLowerCSafe()))
{
return UserAgentEnum.IPad;
}
else if (userAgent.Contains(UserAgentEnum.Nokia.ToString().ToLowerCSafe()))
{
return UserAgentEnum.Nokia;
}
else if (userAgent.Contains(UserAgentEnum.Android.ToString().ToLowerCSafe()))
{
return UserAgentEnum.Android;
}
return UserAgentEnum.Unknown;
}
///
/// Initializes the control properties.
///
protected string CheckUsersRedirection(string userAgent, string smallUrl, string largeUrl)
{
// Split user specified user agents
string[] largeDevices = OtherLargeDevices.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
string[] smallDevices = OtherSmallDevices.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
userAgent = userAgent.ToLowerCSafe();
// Check large devices
if (largeDevices.Length > 0)
{
// Check all specified devices for match
foreach (string t in largeDevices)
{
if (userAgent.Contains(t.ToLowerCSafe()))
{
return largeUrl;
}
}
}
// Check small devices
if (smallDevices.Length > 0)
{
// Check all specified devices for match
foreach (string t in smallDevices)
{
if (userAgent.Contains(t.ToLowerCSafe()))
{
return smallUrl;
}
}
}
// Check if visitor use mobile device
if (BrowserHelper.IsMobileDevice())
{
return smallUrl;
}
return string.Empty;
}
#endregion
}