DISCUZ!NT源码分析

转自:http://www.gkxsn.com/63411865897156250086.html

首先,从Discuz.Web应用程序看起,打开Index.aspx文件,你一定会感觉到很奇异,竟然只有简单的一行代码,然而你设置它为起始页的时候,显示的竟然是论坛首页,这时你就应该想到是URL重写的问题了。那我们就到Web.config去一探究竟,会发现如下代码:
1<httpModules>
2   <add type="Discuz.Forum.HttpModule, Discuz.Forum" name="HttpModule" />
3</httpModules>
 如何熟悉httpModule的朋友,看到这段代码,一定会到Discuz.Forum类库下面的HttpModule类看个究竟。打开HttpModule类以后,先查看如下代码:
 
1public void Init(HttpApplication context)
2{
3    OnlineUsers.ResetOnlineList();//这是复位在线用户表,在此先不研究。
4    context.BeginRequest += new EventHandler(ReUrl_BeginRequest);//最主要就是这句,给应用程序开始请求事件绑定了ReUrl_BeginRequest方法,URL绑定就在这方法中
5     
6}
我们来仔细研究下ReUrl_BeginRequest方法:
 
  1/// <summary>
  2/// 重写Url
  3/// </summary>
  4/// <param name="sender">事件的源</param>
  5/// <param name="e">包含事件数据的 EventArgs</param>
  6private void ReUrl_BeginRequest(object sender, EventArgs e)
  7{
  8    BaseConfigInfo baseconfig = BaseConfigProvider.Instance();
  9    if (baseconfig == null)
 10    {
 11        return;
 12    }
 13    GeneralConfigInfo config = GeneralConfigs.GetConfig();
 14    HttpContext context = ((HttpApplication)sender).Context;
 15    string forumPath = baseconfig.Forumpath.ToLower();
 16
 17    string requestPath = context.Request.Path.ToLower();
 18
 19    //使用版块
 20     if ((config.Iisurlrewrite == 1 || config.Aspxrewrite == 1) && requestPath.EndsWith("/list.aspx") &&
 21         requestPath.IndexOf("/archiver/") < 0 && requestPath.IndexOf("/install/") < 0 && requestPath.IndexOf("/upgrade/") < 0 &&
 22         requestPath.IndexOf("/admin/") < 0 && requestPath.IndexOf("/aspx/") < 0 && requestPath.IndexOf("/tools/") < 0 &&
 23         requestPath.IndexOf("/space/") < 0)
 24       {
 25           requestPath = requestPath.StartsWith("/") ? requestPath : "/" + requestPath;
 26           // 当前样式id
 27           string strTemplateid = config.Templateid.ToString();
 28           if (Utils.InArray(Utils.GetCookie(Utils.GetTemplateCookieName()), Templates.GetValidTemplateIDList()))
 29           {
 30               strTemplateid = Utils.GetCookie(Utils.GetTemplateCookieName());
 31           }
 32
 33           string[] path = requestPath.Replace(BaseConfigs.GetForumPath, "/").Split('/');
 34
 35           //当使用伪aspx, 如:/版块别名/1(分页)等.
 36           if (path.Length > 1 && !Utils.StrIsNullOrEmpty(path[1]))
 37           {                    
 38               int forumid = 0;
 39               foreach (Discuz.Entity.ForumInfo foruminfo in Forums.GetForumList())
 40               {
 41                  if (path[1].ToLower() == foruminfo.Rewritename.ToLower())
 42                        {
 43                            forumid = foruminfo.Fid;
 44                            break;
 45                        }
 46                    }
 47                    if (forumid > 0)
 48                    {
 49                        string newUrl = "forumid=" + forumid;
 50                        if (path.Length > 2 && !Utils.StrIsNullOrEmpty(path[2]) && path[2] != "list.aspx")
 51                        {
 52                            newUrl += "&page=" + path[2];
 53                        }
 54
 55                        //通过参数设置指定模板
 56                        if (config.Specifytemplate > 0)
 57                        {
 58                            strTemplateid = SelectTemplate(strTemplateid, "showforum.aspx", newUrl);
 59                        }
 60                        context.RewritePath(forumPath + "aspx/" + strTemplateid + "/showforum.aspx", "/", newUrl + "&selectedtemplateid=" + strTemplateid);
 61                        return;
 62                    }
 63                }
 64                context.Response.Redirect(baseconfig.Forumpath + "tools/error.htm?templatepath=" + Templates.GetTemplateItem(Utils.StrToInt(strTemplateid, 0)).Directory + "&msg=" + Utils.UrlEncode("您请求的版块信息无效!"));
 65                return;
 66            }
 67            
 68            if (requestPath.StartsWith(forumPath))
 69            {
 70                if (requestPath.Substring(forumPath.Length).IndexOf("/") == -1)
 71                {
 72                    // 当前样式id
 73                    string strTemplateid = config.Templateid.ToString();
 74                    if (Utils.InArray(Utils.GetCookie(Utils.GetTemplateCookieName()), Templates.GetValidTemplateIDList()))
 75                    {
 76                        strTemplateid = Utils.GetCookie(Utils.GetTemplateCookieName());
 77                    }
 78
 79                    if (requestPath.EndsWith("/index.aspx"))
 80                    {
 81                        if (config.Indexpage == 0)
 82                        {
 83                            if (config.BrowseCreateTemplate == 1)
 84                            {
 85                                CreateTemplate(forumPath, Templates.GetTemplateItem(int.Parse(strTemplateid)).Directory, "forumindex.aspx", int.Parse(strTemplateid));
 86                            }
 87                            context.RewritePath(forumPath + "aspx/" + strTemplateid + "/forumindex.aspx");
 88                        }
 89                        else
 90                        {
 91                            if (config.BrowseCreateTemplate == 1)
 92                            {
 93                                CreateTemplate(forumPath, Templates.GetTemplateItem(int.Parse(strTemplateid)).Directory, "website.aspx", int.Parse(strTemplateid));
 94                            }
 95                            context.RewritePath(forumPath + "aspx/" + strTemplateid + "/website.aspx");
 96                        }
 97
 98                        return;
 99                    }
100
101
102                    //当使用伪aspx, 如:showforum-1.aspx等.
103                    if (config.Aspxrewrite == 1)
104                    {
105                        foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
106                        {
107                            if (Regex.IsMatch(requestPath, url.Pattern, Utils.GetRegexCompiledOptions() | RegexOptions.IgnoreCase))
108                            {
109                                string newUrl = Regex.Replace(requestPath.Substring(context.Request.Path.LastIndexOf("/")), url.Pattern, url.QueryString, Utils.GetRegexCompiledOptions() | RegexOptions.IgnoreCase);
110                                if (config.BrowseCreateTemplate == 1)
111                                {
112                                    CreateTemplate(forumPath, Templates.GetTemplateItem(int.Parse(strTemplateid)).Directory, url.Page.Replace("/", ""), int.Parse(strTemplateid));
113                                }
114                                //通过参数设置指定模板
115                                if (config.Specifytemplate > 0)
116                                {
117                                    strTemplateid = SelectTemplate(strTemplateid, url.Page, newUrl);
118                                }
119                                context.RewritePath(forumPath + "aspx/" + strTemplateid + url.Page, string.Empty, newUrl + "&selectedtemplateid=" + strTemplateid);
120
121                                return;
122                            }
123                        }
124                    }
125
126                    if (config.BrowseCreateTemplate == 1)
127                    {
128                        if (requestPath.IndexOf("showtemplate.aspx") != -1)
129                        {
130                            CreateTemplate(forumPath,
131                                Templates.GetTemplateItem(DNTRequest.GetInt("templateid", 1)).Directory,
132                                config.Indexpage == 0 ? "forumindex.aspx" : "website.aspx",
133                                DNTRequest.GetInt("templateid", 1)); //当跳转模板页时,生成目标文件
134                        }
135                        CreateTemplate(forumPath, Templates.GetTemplateItem(int.Parse(strTemplateid)).Directory, requestPath.Substring(context.Request.Path.LastIndexOf("/")).Replace("/", ""), int.Parse(strTemplateid));
136
137                    }
138                    //通过参数设置指定模板
139                    if (config.Specifytemplate > 0)
140                    {
141                        strTemplateid = SelectTemplate(strTemplateid, requestPath, context.Request.QueryString.ToString());
142                    }
143                    context.RewritePath(forumPath + "aspx/" + strTemplateid + requestPath.Substring(context.Request.Path.LastIndexOf("/")), string.Empty, context.Request.QueryString.ToString() + "&selectedtemplateid=" + strTemplateid);
144                }
145
146                else if (requestPath.StartsWith(forumPath + "archiver/"))
147                {
148                    //当使用伪aspx, 如:showforum-1.aspx等.
149                    if (config.Aspxrewrite == 1)
150                    {
151                        string path = requestPath.Substring(forumPath.Length + 8);
152                        foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
153                        {
154                            if (Regex.IsMatch(path, url.Pattern, Utils.GetRegexCompiledOptions() | RegexOptions.IgnoreCase))
155                            {
156                                string newUrl = Regex.Replace(path, url.Pattern, url.QueryString, Utils.GetRegexCompiledOptions() | RegexOptions.IgnoreCase);
157
158                                context.RewritePath(forumPath + "archiver" + url.Page, string.Empty, newUrl);
159                                return;
160                            }
161                        }
162
163                    }
164                    return;
165                }
166                else if (requestPath.StartsWith(forumPath + "tools/"))
167                {
168                    //当使用伪aspx, 如:showforum-1.aspx等.
169                    if (config.Aspxrewrite == 1)
170                    {
171                        string path = requestPath.Substring(forumPath.Length + 5);
172                        foreach (SiteUrls.URLRewrite url in SiteUrls.GetSiteUrls().Urls)
173                        {
174                            if (Regex.IsMatch(path, url.Pattern, Utils.GetRegexCompiledOptions() | RegexOptions.IgnoreCase))
175                            {
176                                string newUrl = Regex.Replace(path, url.Pattern, Utils.UrlDecode(url.QueryString), Utils.GetRegexCompiledOptions() | RegexOptions.IgnoreCase);
177
178                                context.RewritePath(forumPath + "tools" + url.Page, string.Empty, newUrl);
179                                return;
180                            }
181                        }
182                    }
183                    return;
184                }
185                else if (requestPath.StartsWith(forumPath + "upload/") || requestPath.StartsWith(forumPath + "space/upload/"))
186                {
187                    context.RewritePath(forumPath + "index.aspx");
188                    return;
189                }
190
191            }
192        }
posted @ 2014-09-28 15:46  jiang_jiang  阅读(703)  评论(0)    收藏  举报