日期:2010-11-25  浏览次数:20522 次

/*
 * 
 * 防盗链IHttpHandler
 * 
 * 
 * 增加了对文件关键字的选择(即仅对文件名存在某些关键字或不存在某些关键字进行过滤)
 * 设置web.config中<appSettings>节以下值
 * string eWebapp_NoLink    如果文件名符合该正确表态式将进行过滤(不设置对所有进行过滤)
 * string eWebapp_AllowLink            如果文件名符合该正确表态式将不进行过滤(优先权高于AllowLink,不设置则服从AllowLink)
 * bool eWebapp_ AllowOnlyFile        如果为False,(默认true)则不允许用户直接对该文件进行访问建议为true
 * 
 * 
 * :)以下设置均可省略,设置只是为了增加灵活性与体验
 * eWebapp_NoLink_Message    错误信息提示:默认为Link From:域名
 * eWebapp_Error_Width        错误信息提示图片宽
 * eWebapp_Error_Height        错误信息提示图片高
 * 
 * 
 * 
 *
 * http://ewebapp.net 
 */


using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;

namespace eWebapp
{
    /// <summary>
    /// 防盗链IHttpHandler
    /// 参考http://www.softat.org/archiver/tid-52114.html
    ///
    /// </summary>
    public class NoLink : IHttpHandler
    {
        private string eWebapp_NoLink = string.Empty;
        private string eWebapp_AllowLink = string.Empty;
        private bool eWebapp_AllowOnlyFile = true;

        private string eWebapp_NoLink_Message = string.Empty;
        private bool error = false;

        public NoLink()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public void ProcessRequest(HttpContext context)
        {
            eWebapp_NoLink_Message = ConfigurationSettings.AppSettings["eWebapp_NoLink_Message"];
            
            
            string myDomain = string.Empty;

            error = errorLink(context,out myDomain);    

            if(Empty(eWebapp_NoLink_Message)) 
            {
                eWebapp_NoLink_Message = "Link from :" + myDomain;
            }

            if(error)
            {
                //Jpg(context.Response,eWeb