HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/RMourik/bassol.nl/wwwroot/CMSWebParts/General/RandomRedirection.ascx.cs
using System;

using CMS.Helpers;
using CMS.PortalControls;
using CMS.PortalEngine;

public partial class CMSWebParts_General_RandomRedirection : CMSAbstractWebPart
{
    #region Webpart properties

    /// <summary>
    /// List of URLs to where webpart could redirect.
    /// </summary>
    public string RedirectionURLs
    {
        get
        {
            return ValidationHelper.GetString(GetValue("RedirectionURLs"), "");
        }
        set
        {
            SetValue("RedirectionURLs", value);
        }
    }

    #endregion


    #region Webpart methods

    /// <summary>
    /// Content loaded event handler.
    /// </summary>
    public override void OnContentLoaded()
    {
        base.OnContentLoaded();
        SetupControl();
    }


    /// <summary>
    /// Initializes the control properties.
    /// </summary>
    protected void SetupControl()
    {
        if (!StopProcessing)
        {
            if ((RedirectionURLs.Trim().Length > 0) &&
                PortalContext.ViewMode.IsLiveSite())
            {
                // Parse URLs string
                string[] URLs = RedirectionURLs.Trim().Split(new String[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

                if (URLs.Length > 0)
                {
                    // Generate random integer index to array
                    int rndID = new Random().Next(0, URLs.Length);
                    string newURL = URLHelper.ResolveUrl(URLs[rndID].Trim());
                    if ((RequestContext.CurrentURL != newURL) &&
                        (URLHelper.GetAbsoluteUrl(RequestContext.CurrentURL) != newURL))
                    {
                        URLHelper.ResponseRedirect(newURL);
                    }
                }
            }
        }
    }


    /// <summary>
    /// Reloads the control data.
    /// </summary>
    public override void ReloadData()
    {
        base.ReloadData();
        SetupControl();
    }

    #endregion
}