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

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

public class WebService : 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><![CDATA[ " + 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)
    {
        string result = "";
        string RegName = context.Request.Params["UserName"];
        UserBLL _user = new UserBLL();
        DataTable dt = _user.GetUserByUserName(RegName);
        if (dt.Rows.Count > 0)
            result = "该用户名已经被注册";
        else
            result = "该用户名可以注册";
        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"];
        try
        {
            doc.Load(m_RssUrl);
        }
        catch
        {
            doc.InnerXml = "<item><title>来源页面出错请稍候试！</title><link>" + m_RssUrl + "</link><description>来源页面地址不正确或来源页面出错</description></item>";

        }
        context.Response.ContentType = "text/xml";
        if (doc.InnerXml.Contains("encoding=\"gbk\"") || doc.InnerXml.Contains("encoding=\"GBK\""))
        {
            doc.InnerXml = doc.InnerXml.Replace("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);
            string _spaceArticleType = "Rss";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                switch (dt.Rows[i]["ClassID"].ToString())
                {
                    case "1":
                        _spaceArticleType = "Rss";
                        break;
                    case "2":
                        _spaceArticleType = "Js";
                        break;
                    default:
                        _spaceArticleType = "Rss";
                        break;                        

                }              
                   
                _result.Append("<item><id>DragBox_" + dt.Rows[i]["ID"].ToString() + "</id><link><![CDATA[" + dt.Rows[i]["RssUrl"].ToString() + "]]></link><title>" + dt.Rows[i]["ClassName"].ToString() + "</title><col>"+dt.Rows[i]["OrderNum"].ToString().Substring(0,1)+"</col><row>"+dt.Rows[i]["OrderNum"].ToString().Substring(1,1)+"</row><left>0</left><top>0</top><width>0</width><height>0</height><nums>" + dt.Rows[i]["RssNum"].ToString() + "</nums><articleType>" + _spaceArticleType + "</articleType></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";
            if (context.Session["CheckCode"].ToString().ToLower() == m_checkCode.ToLower())
                context.Response.Write("true");
            else
                context.Response.Write("false");

        }

    }
   
}