<%@ WebHandler Language="C#" Class="PassportService" %>

using System;
using System.Data;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Xml;
using System.Web.SessionState;

public class PassportService : IHttpHandler, IRequiresSessionState
{
    private int m_UserID=0;
    public void ProcessRequest (HttpContext context) 
    {
        context.Response.ContentType = "text/xml";
        string action = context.Request.Params["Action"];
        switch (action)
        {
            case "CheckReg":
                CheckReg(context);
                break;
            case "GetAnnounce":
                GetAnnounce(context);
                break;           
            case "GetRssContent":
                GetRssContent(context);
                break;
            case "GetPublicRssList":
                GetPublicRssList(context);
                break;
            case "CheckCode":
                CheckValidateCode(context);
                break;               
            default:
                HowaveFuncion _chk = new HowaveFuncion();
                if (_chk.CheckUserLogined())
                {
                    m_UserID = Convert.ToInt32(_chk.UserID);
                    switch (action)
                    {
                        case "AddRss":
                            AddRss(context);
                            break;
                        case "GetMyRssList":
                            GetMyRssList(context, m_UserID);
                            break;                     
                        default:
                            break;

                    }
                   
                }
                break;

        }
        context.Response.Flush();
        context.Response.Close();       
       
    }

    private void AddRss(HttpContext context)
    {
        string RssURL = context.Request.Params["RssUrl"];
        string RssName = context.Request.Params["RssName"];
        string result="";
        if (RssName != "" && RssURL != "")
        {
            MySpace _rss = new MySpace();
            if (_rss.AddRss(m_UserID, RssURL, RssName,10,1,11,true,false))            
                result = "<rss><url>" + RssURL + "</url><name>" + RssName + "</name></rss>";           
            else
                result = "<rss><url>err</url><name>err</name></rss>";         
            context.Response.Write(result);
            

        }
    }

    private void GetMyRssList(HttpContext context,int UserID)
    {
        if (UserID > 0)
        {
            MySpace _rss = new MySpace();
            DataTable dt = _rss.GetMyRss(UserID);
            if (dt.Rows.Count > 0)
            {
                StringBuilder _result = new StringBuilder("<rss>",300);                
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    _result.Append("<item><url>" + dt.Rows[i]["RssUrl"].ToString() + "</url><name>"+dt.Rows[i]["ClassName"].ToString()+"</name></item>");

                }
                _result.Append("</rss>");
                context.Response.Write(_result.ToString());

            }
        }

    }

    private void CheckReg(HttpContext context)
    {
        HowaveFuncion _func = new HowaveFuncion();
        string result="";
        string RegName = _func.ReplaceBadChar(context.Request.Params["UserName"]);
        string RegNameMin = _func.ReplaceBadChar(context.Request.Params["RegNameMin"]);
        string RegNameMax = _func.ReplaceBadChar(context.Request.Params["RegNameMax"]);
        int i_RegNameMin = Int32.Parse(RegNameMin);
        int i_RegNameMax = Int32.Parse(RegNameMax);
        UserBLL _user = new UserBLL();
        DataTable dt = _user.GetUserByUserName(RegName);
        if (dt.Rows.Count > 0)
        {
            result = "false";
        }
        else
        {
            if (_func.GetStrLen(RegName) < i_RegNameMin || _func.GetStrLen(RegName) > i_RegNameMax)
            {
                result = "lengthwrong";
            }
            else
            {
                result = "true";
            }
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write(result);

    }

    private void GetRssContent(HttpContext context)
    {
        XmlDocument doc = new XmlDocument();
        string m_RssUrl=context.Request.Params["rssurl"];
        doc.Load(m_RssUrl);
        context.Response.ContentType = "text/xml";
        if (doc.InnerXml.Contains("encoding=\"gbk\""))
            doc.InnerXml = doc.InnerXml.Replace("encoding=\"gbk\"", "");
        context.Response.Write(doc.InnerXml);

    }
  
    public bool IsReusable {
        get {
            return false;
        }
    }
    private void GetAnnounce(HttpContext context)
    {
        AnnounceBLL announce = new AnnounceBLL();
        DataTable dt = announce.GetAnnounces();
        if (dt.Rows.Count > 0)
        {
            StringBuilder _result = new StringBuilder("<rss>", 300);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if(Convert.ToDateTime(dt.Rows[i]["AnnounceEnd"])>DateTime.Now)
                    _result.Append("<item><url>ShowResource.aspx?DefaultValue=" + dt.Rows[i]["AnnounceID"].ToString() + "</url><name>" + dt.Rows[i]["AnnounceTitle"].ToString() + "</name></item>");

            }
            _result.Append("</rss>");
            context.Response.Write(_result.ToString());

        }

    }
    private void GetPublicRssList(HttpContext context)
    {
        MySpace _rss = new MySpace();
        DataTable dt = _rss.GetMyRss(1);
        if (dt.Rows.Count > 0)
        {
            StringBuilder _result = new StringBuilder("<rss>", 300);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                _result.Append("<item><url>" + dt.Rows[i]["RssUrl"].ToString() + "</url><name>" + dt.Rows[i]["ClassName"].ToString() + "</name></item>");

            }
            _result.Append("</rss>");
            context.Response.Write(_result.ToString());

        }

    }

    private void CheckValidateCode(HttpContext context)
    {
        string m_checkCode = context.Request["ValidateCode"];
        if (context.Session["CheckCode"] != null)
        {
            context.Response.ContentType = "text/plain";
            string Session_checkCode = context.Session["CheckCode"].ToString();
            if (context.Session["CheckCode"].ToString().ToLower() == m_checkCode.ToLower())
                context.Response.Write("true");
            else
                context.Response.Write("false");

        }

    }

}