using CallCenter.QuartzService;
using CallCenter.Utility;
using CallCenterApi.Common;
using CallCenterApi.Interface.Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Script.Serialization;
using System.Web.Security;

namespace CallCenterApi.Interface
{
    public class MvcApplication : System.Web.HttpApplication
    {
   
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //clq 增加异常日志记录 自定义 HandleErrorAttribute
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);


        }
     

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            var context = app.Context;
            if (context == null) throw new ArgumentNullException("context");

            Dictionary<string, string> Params = new Dictionary<string, string>();
            Params.Add("request_url", context.Request.Url.ToString());

            bool issafe = true;
            foreach (var key in context.Request.Params.AllKeys)
            {
                if (key == "ALL_HTTP")
                {
                    break;
                }
                Params.Add(key, context.Request.Params[key]);
                try
                {
                    string paras = context.Request.Params[key].ToString();
                    if (!paras.Contains("data:image/png;base64,"))
                    {
                        Utils.SqlFilter(paras);
                    }
                }
                catch
                {
                    issafe = false;
                    break;
                }
            }
            if (!issafe)
            {
                context.Response.Write(new AjaxResult { state = ResultTypes.error.ToString(), message = "非法输入！" }.ToJson());
                context.Response.End();
            }

            var log = LogFactory.GetLogger(this.GetType().ToString());
            if (Configs.GetValue("IsLog").ToString() == "1")
            {
                log.Debug(Params.ToJson());
            }
        }
    }
}
