//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;

namespace CommunityServer.Components
{
	/// <summary>
	/// Summary description for CSValidationModule.
	/// </summary>
	public class CSValidationModule : ICSModule
	{
		public CSValidationModule()
		{
			//
			// TODO: Add constructor logic here
			//
        }
        #region ICSModule Members

        public void Init(CSApplication csa, System.Xml.XmlNode node)
        {
            csa.PreUserUpdate +=new CSUserEventHandler(csa_PreUserUpdate);
        }

        #endregion

        private void csa_PreUserUpdate(User user, CSEventArgs e)
        {
            if(e.State == ObjectState.Create || e.State == ObjectState.Update)
            {
                if(user.HasProfile && !user.Profile.IsReadOnly)
                {
                    user.Profile.Bio = HtmlScrubber.Clean(user.Profile.Bio,true,true);

                    //Need to clean up sig processing. Set on formatted calls another formatting method.
                    user.Profile.Signature = HtmlScrubber.Clean(user.Profile.Signature, true, true); //Transforms.StripHtmlXmlTags(user.Profile.Signature);

                    if( CSContext.Current.SiteSettings.EnableCensorship ) 
                    {
                        user.Profile.SignatureFormatted = Transforms.CensorPost( user.Profile.Signature );
                    }

                    user.Profile.AolIM = Globals.HtmlEncode(user.Profile.AolIM);
                    user.Profile.PublicEmail = Globals.HtmlEncode(user.Profile.PublicEmail);
                    user.Profile.IcqIM = Globals.HtmlEncode(user.Profile.IcqIM);
                    user.Profile.Interests = Globals.HtmlEncode(user.Profile.Interests);
                    user.Profile.Location = Globals.HtmlEncode(user.Profile.Location);
                    user.Profile.MsnIM = Globals.HtmlEncode(user.Profile.MsnIM);
                    user.Profile.Occupation = Globals.HtmlEncode(user.Profile.Occupation);
                    user.Profile.WebAddress = Globals.HtmlEncode(user.Profile.WebAddress);
                    user.Profile.WebLog = Globals.HtmlEncode(user.Profile.WebLog);
                    user.Profile.YahooIM = Globals.HtmlEncode(user.Profile.YahooIM);
                    user.Profile.CommonName = Globals.HtmlEncode(user.Profile.CommonName);
                    if(!Globals.IsNullorEmpty(user.Profile.CommonName) && user.Profile.CommonName.Length > 50)
                    {
                        user.Profile.CommonName = user.Profile.CommonName.Substring(0,50);
                    }
                    
                 
                }
                user.Email = Globals.HtmlEncode(user.Email);
                user.AvatarUrl = Globals.HtmlEncode(user.AvatarUrl);
                
            }
        }
    }
}
