Exemplo n.º 1
0
        public virtual void SITE_INIT()
        {
            // bots are bad, block bots
            List<string> whitelist = new List<string>();
            if(ConfigurationManager.AppSettings["bot-white-list"] != null) {
                if(Cache["bot-white-list"] != null)
                {
                    whitelist = (List<String>)Cache["bot-white-list"];
                }
                else
                {
                    var filepath = Server.MapPath(ConfigurationManager.AppSettings["bot-white-list"]);
                    if(File.Exists(filepath))
                    {
                        whitelist = new List<string>();
                        using(StreamReader sreader = new StreamReader(filepath))
                        {
                            string line = null;
                            while((line = sreader.ReadLine()) != null)
                            {
                                whitelist.Add(line);
                            }
                        }
                        var cachedependency = new CacheDependency(filepath);
                        Cache.Insert("bot-white-list", whitelist, cachedependency, DateTime.Now.AddDays(3), System.Web.Caching.Cache.NoSlidingExpiration);
                    }
                }
            }
            var useragent = Request.ServerVariables["HTTP_USER_AGENT"];
            if(!String.IsNullOrEmpty(useragent))
            {
                if (!whitelist.Contains(useragent) && useragent.Contains("bot"))
                {
                    _validUserAgent = false;
                    TMPLT.AddTag("ISBot", "YES");
                }
            }

            _useCdn = (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["use-cdn"]) && ConfigurationManager.AppSettings["use-cdn"] == "true");
            _useCache = (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["cache-enabled"]) && ConfigurationManager.AppSettings["cache-enabled"] == "true");
            _cacheMinutes = !String.IsNullOrEmpty(ConfigurationManager.AppSettings["cache-expires-minutes"]) ? int.Parse(ConfigurationManager.AppSettings["cache-expires-minutes"]) : 60;

            _cdnSite = ConfigurationManager.AppSettings["project-cdn-path"];
            _clientFlxpath = ConfigurationManager.AppSettings["projectFlxTemplates"];

            _SiteMap = PageHeirarchyCombined.Equals("sitemap") || PageHeirarchyCombined.Equals("sitemapxml");

            if (_SiteMap)
                return;

            if (_useCdn)
            {
                if (_resources == null)
                    throw new Exception("Expecting CDN Resources");
            }
            else
            {
                if (String.IsNullOrEmpty(_clientFlxpath))
                    throw new Exception("Missing projectFlxTemplates in web config");

                // TODO: cache result of file resources
                _resources = FileResources.getFileResources(Server.MapPath(_clientFlxpath), (Request.ServerVariables["HTTPS"] == "on") ? "https://" : "http://" + Request.ServerVariables["HTTP_HOST"], _clientFlxpath);
            }

            XmlDocument content = new XmlDocument();
            content.XmlResolver = new XmlUrlResolver();
            string resourcecontentpath = null;

            XmlNode current = null, context = null;
            resourcecontentpath = getXmlResources(content, ref current);

            TMPLT.AddXML("client-context", getXmlContext(content));

            var nsmgr = new XmlNamespaceManager(current.OwnerDocument.NameTable);
            nsmgr.AddNamespace("wbt", "myWebTemplater.1.0");
            nsmgr.AddNamespace("sbt", "mySiteTemplater.1.0");
            nsmgr.AddNamespace("pbt", "myPageTemplater.1.0");

            var newAtt = current.OwnerDocument.CreateAttribute("wbt", "loggedonuser", nsmgr.LookupNamespace("wbt"));
            newAtt.Value = (current.SelectSingleNode("ancestor-or-self::content[@loggedonuser='******'] | ancestor-or-self::LoggedOn | ancestor-or-self::LoggedIn | ancestor-or-self::LoggedInUser") != null).ToString().ToLower();
            current.Attributes.Append(newAtt);
            newAtt = current.OwnerDocument.CreateAttribute("wbt", "authenticateduser", nsmgr.LookupNamespace("wbt"));
            newAtt.Value = (current.SelectSingleNode("ancestor-or-self::content[@authenticateduser='******'] | ancestor-or-self::Authenticated") != null).ToString().ToLower();
            current.Attributes.Append(newAtt);

            wbtQuery(current, resourcecontentpath);

            TMPLT.AddXML("client", current);
            if (content == null)
                throw new Exception("Project FLX Content not found!  Expecting ProjectFLX XmlDocument resource for the request - and/or - missing ProjectFLX default XmlDocument resource at: /ProjectFlx/ProjectFlx.Xml");

            string[] paths = { "", String.IsNullOrEmpty(resourcecontentpath) ? "SKIP__RESOURCE" : resourcecontentpath};

            #region embed required scripts
            foreach (string pickup in paths)
            {
                if (pickup == "SKIP__RESOURCE")
                    continue;

                if (_resources.Exists(Utility.Paths.CombinePaths(pickup, "/script/required.txt")))
                {
                    StringReader txtreader;
                    if (_useCdn)
                    {
                        txtreader = new StringReader(Utility.Web.getWebResource(_resources.FullWebPath(_resources.IndexOf)));
                    }
                    else
                    {
                        // load required scripts
                        using (StreamReader reader = new StreamReader(Server.MapPath(_resources.AbsolutePath(_resources.IndexOf))))
                        {
                            txtreader = new StringReader(reader.ReadToEnd());
                        }
                    }
                    var line = txtreader.ReadLine();
                    while (line != null)
                    {
                        if (line.EndsWith(".js"))
                            TMPLT.AddBrowserPageItem("SCRIPT", line.Replace("\\", "/"));
                        line = txtreader.ReadLine();
                    }
                }
            }
            #endregion

            #region embed inline content xml pbt:javascript and pbt:style
            if (current != null)
            {
                foreach (XmlNode node in current.SelectNodes("pbt:*", nsmgr))
                {
                    if (node != null)
                    {
                        var nodes = node.SelectNodes("src", nsmgr);
                        foreach (XmlNode srcNode in nodes)
                        {
                            switch (node.LocalName)
                            {
                                case "javascript":
                                    TMPLT.AddBrowserPageItem("SCRIPT", Utility.Paths.CombinePaths(node.Attributes["base"] == null ? "" : node.Attributes["base"].Value, srcNode.InnerText));
                                    break;
                                case "style":
                                    TMPLT.AddBrowserPageItem("STYLE", Utility.Paths.CombinePaths(node.Attributes["base"] == null ? "" : node.Attributes["base"].Value, srcNode.InnerText));
                                    break;
                            }
                        }
                    }
                }
            }
            #endregion

            // pickup style from local content
            foreach (string s in _resources.collectResources("style", ".css"))
                TMPLT.AddBrowserPageItem("STYLE", (_useCdn) ? Utility.Paths.CombinePaths(_resources.Host, s) : s);

            foreach (string s in _resources.collectResources(Utility.Paths.CombinePaths(resourcecontentpath,"style"), ".css"))
                TMPLT.AddBrowserPageItem("STYLE", (_useCdn) ? Utility.Paths.CombinePaths(_resources.Host, s) : s);

            // pickup script from local content
            foreach (string s in _resources.collectResources("script", ".js"))
                TMPLT.AddBrowserPageItem("SCRIPT", (_useCdn) ? Utility.Paths.CombinePaths(_resources.Host, s) : s);

            foreach (string s in _resources.collectResources(Utility.Paths.CombinePaths(resourcecontentpath, "script"), ".js"))
                TMPLT.AddBrowserPageItem("SCRIPT", (_useCdn) ? Utility.Paths.CombinePaths(_resources.Host, s) : s);

            // meta tags
            string[] meta = { "DESCRIPTION", "KEYWORDS", "TITLE" };
            foreach (var m in meta)
            {
                var filename = m.ToLower() + ".txt";
                if(_resources.Exists(Utility.Paths.CombinePaths(resourcecontentpath, "meta",  filename)))
                {
                    if (_useCdn)
                    {
                        TMPLT.AddBrowserPageItem(m, Utility.Web.getWebResource(_resources.FullWebPath(_resources.IndexOf)));
                    }
                    else
                    {
                        using (StreamReader r = new StreamReader(Server.MapPath(_resources.AbsolutePath(_resources.IndexOf))))
                        {
                            TMPLT.AddBrowserPageItem(m, r.ReadToEnd());
                        }
                    }
                }
                else if (_resources.Exists(Utility.Paths.CombinePaths("meta", filename)))
                {
                    if (_useCdn)
                    {

                        TMPLT.AddBrowserPageItem(m, Utility.Web.getWebResource(_resources.FullWebPath(_resources.IndexOf)));
                    }
                    else
                    {
                        using (StreamReader r = new StreamReader(Server.MapPath(_resources.AbsolutePath(_resources.IndexOf))))
                        {
                            TMPLT.AddBrowserPageItem(m, r.ReadToEnd());
                        }
                    }
                }

            }
        }
Exemplo n.º 2
0
 private void recurseForSiteMap(FileResources resources)
 {
     foreach (var s in resources.collectResources(".xml"))
     {
         TMPLT.AddXML(Utility.Web.getWebResource(Utility.Paths.CombinePaths(_resources.Host, s)));
     }
 }