/// <summary> /// Copies the base properties from a source PageViewUserAgent object /// </summary> /// <param name="source">The source.</param> public void CopyPropertiesFrom(PageViewUserAgent source) { this.Id = source.Id; this.Browser = source.Browser; this.ClientType = source.ClientType; this.ForeignGuid = source.ForeignGuid; this.ForeignKey = source.ForeignKey; this.OperatingSystem = source.OperatingSystem; this.UserAgent = source.UserAgent; this.Guid = source.Guid; this.ForeignId = source.ForeignId; }
/// <summary> /// Determine the logical page being requested by evaluating the routedata, or querystring and /// then loading the appropriate layout (ASPX) page /// </summary> /// <param name="requestContext"></param> /// <returns></returns> System.Web.IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext) { if (requestContext == null) { throw new ArgumentNullException("requestContext"); } try { string pageId = ""; int routeId = 0; var parms = new Dictionary <string, string>(); // Pages using the default routing URL will have the page id in the RouteData.Values collection if (requestContext.RouteData.Values["PageId"] != null) { pageId = (string)requestContext.RouteData.Values["PageId"]; } // Pages that use a custom URL route will have the page id in the RouteDate.DataTokens collection else if (requestContext.RouteData.DataTokens["PageId"] != null) { pageId = (string)requestContext.RouteData.DataTokens["PageId"]; routeId = Int32.Parse((string)requestContext.RouteData.DataTokens["RouteId"]); foreach (var routeParm in requestContext.RouteData.Values) { parms.Add(routeParm.Key, (string)routeParm.Value); } } // If page has not been specified get the site by the domain and use the site's default page else { SiteCache site = SiteCache.GetSiteByDomain(requestContext.HttpContext.Request.Url.Host); // if not found use the default site if (site == null) { site = SiteCache.Read(SystemGuid.Site.SITE_ROCK_INTERNAL.AsGuid()); } if (site != null) { // If site has has been enabled for mobile redirect, then we'll need to check what type of device is being used if (site.EnableMobileRedirect) { bool redirect = false; // get the device type string u = requestContext.HttpContext.Request.UserAgent; var clientType = PageViewUserAgent.GetClientType(u); // first check if device is a mobile device if (clientType == "Mobile") { redirect = true; } // if not, mobile device and tables should be redirected also, check if device is a tablet if (!redirect && site.RedirectTablets) { if (clientType == "Tablet") { redirect = true; } } if (redirect) { if (site.MobilePageId.HasValue) { pageId = site.MobilePageId.Value.ToString(); } else if (!string.IsNullOrWhiteSpace(site.ExternalUrl)) { requestContext.HttpContext.Response.Redirect(site.ExternalUrl); return(null); } } } if (string.IsNullOrWhiteSpace(pageId)) { if (site.DefaultPageId.HasValue) { pageId = site.DefaultPageId.Value.ToString(); } if (site.DefaultPageRouteId.HasValue) { routeId = site.DefaultPageRouteId.Value; } } } if (string.IsNullOrEmpty(pageId)) { throw new SystemException("Invalid Site Configuration"); } } PageCache page = null; if (!string.IsNullOrEmpty(pageId)) { int pageIdNumber = 0; if (Int32.TryParse(pageId, out pageIdNumber)) { page = PageCache.Read(pageIdNumber); } } if (page == null) { // try to get site's 404 page SiteCache site = SiteCache.GetSiteByDomain(requestContext.HttpContext.Request.Url.Host); if (site != null && site.PageNotFoundPageId != null) { if (Convert.ToBoolean(GlobalAttributesCache.Read().GetValue("Log404AsException"))) { Rock.Model.ExceptionLogService.LogException( new Exception(string.Format("404 Error: {0}", requestContext.HttpContext.Request.Url.AbsoluteUri)), requestContext.HttpContext.ApplicationInstance.Context); } page = PageCache.Read(site.PageNotFoundPageId ?? 0); } else { // no 404 page found for the site, return the default 404 error page return((System.Web.UI.Page)BuildManager.CreateInstanceFromVirtualPath("~/Http404Error.aspx", typeof(System.Web.UI.Page))); } } string theme = page.Layout.Site.Theme; string layout = page.Layout.FileName; string layoutPath = PageCache.FormatPath(theme, layout); try { // Return the page for the selected theme and layout Rock.Web.UI.RockPage cmsPage = (Rock.Web.UI.RockPage)BuildManager.CreateInstanceFromVirtualPath(layoutPath, typeof(Rock.Web.UI.RockPage)); cmsPage.SetPage(page); cmsPage.PageReference = new PageReference(page.Id, routeId, parms, requestContext.HttpContext.Request.QueryString); return(cmsPage); } catch (System.Web.HttpException) { // The Selected theme and/or layout didn't exist, attempt first to use the layout in the default theme. theme = "Rock"; // If not using the default layout, verify that Layout exists in the default theme directory if (layout != "FullWidth" && !File.Exists(requestContext.HttpContext.Server.MapPath(string.Format("~/Themes/Rock/Layouts/{0}.aspx", layout)))) { // If selected layout doesn't exist in the default theme, switch to the Default layout layout = "FullWidth"; } // Build the path to the aspx file to layoutPath = PageCache.FormatPath(theme, layout); // Return the default layout and/or theme Rock.Web.UI.RockPage cmsPage = (Rock.Web.UI.RockPage)BuildManager.CreateInstanceFromVirtualPath(layoutPath, typeof(Rock.Web.UI.RockPage)); cmsPage.SetPage(page); cmsPage.PageReference = new PageReference(page.Id, routeId, parms, requestContext.HttpContext.Request.QueryString); return(cmsPage); } } catch (Exception ex) { if (requestContext.HttpContext != null) { requestContext.HttpContext.Cache["RockExceptionOrder"] = "66"; requestContext.HttpContext.Cache["RockLastException"] = ex; } System.Web.UI.Page errorPage = (System.Web.UI.Page)BuildManager.CreateInstanceFromVirtualPath("~/Error.aspx", typeof(System.Web.UI.Page)); return(errorPage); } }
/// <summary> /// Execute method to write transaction to the database. /// </summary> public void Execute() { using (var rockContext = new RockContext()) { PageViewService pageViewService = new PageViewService(rockContext); PageViewUserAgentService pageViewUserAgentService = new PageViewUserAgentService(rockContext); PageViewSessionService pageViewSessionService = new PageViewSessionService(rockContext); var userAgent = (this.UserAgent ?? string.Empty).Trim(); if (userAgent.Length > 450) { userAgent = userAgent.Substring(0, 450); // trim super long useragents to fit in pageViewUserAgent.UserAgent } // get user agent info var clientType = PageViewUserAgent.GetClientType(userAgent); Parser uaParser = Parser.GetDefault(); ClientInfo client = uaParser.Parse(userAgent); var clientOs = client.OS.ToString(); var clientBrowser = client.UserAgent.ToString(); // lookup the pageViewUserAgent, and create it if it doesn't exist var pageViewUserAgent = pageViewUserAgentService.Queryable().Where(a => a.UserAgent == userAgent).FirstOrDefault(); if (pageViewUserAgent == null) { pageViewUserAgent = new PageViewUserAgent(); pageViewUserAgent.UserAgent = userAgent; pageViewUserAgent.ClientType = clientType; pageViewUserAgent.OperatingSystem = clientOs; pageViewUserAgent.Browser = clientBrowser; pageViewUserAgentService.Add(pageViewUserAgent); rockContext.SaveChanges(); } else { // check if the user agent properties need to be updated if (clientType != pageViewUserAgent.ClientType || clientOs != pageViewUserAgent.OperatingSystem || clientBrowser != pageViewUserAgent.Browser) { pageViewUserAgent.ClientType = clientType; pageViewUserAgent.OperatingSystem = clientOs; pageViewUserAgent.Browser = clientBrowser; rockContext.SaveChanges(); } } // lookup PageViewSession, and create it if it doesn't exist Guid sessionId = this.SessionId.AsGuid(); int? pageViewSessionId = pageViewSessionService.Queryable().Where(a => a.PageViewUserAgentId == pageViewUserAgent.Id && a.SessionId == sessionId && a.IpAddress == this.IPAddress).Select(a => (int?)a.Id).FirstOrDefault(); if (!pageViewSessionId.HasValue) { var pageViewSession = new PageViewSession(); pageViewSession.PageViewUserAgentId = pageViewUserAgent.Id; pageViewSession.SessionId = sessionId; pageViewSession.IpAddress = this.IPAddress; pageViewSessionService.Add(pageViewSession); rockContext.SaveChanges(); pageViewSessionId = pageViewSession.Id; } PageView pageView = new PageView(); pageViewService.Add(pageView); // obfuscate rock magic token Regex rgx = new Regex(@"rckipid=([^&]*)"); string cleanUrl = rgx.Replace(this.Url, "rckipid=XXXXXXXXXXXXXXXXXXXXXXXXXXXX"); pageView.PageId = this.PageId; pageView.SiteId = this.SiteId; pageView.Url = cleanUrl.Left(500); pageView.DateTimeViewed = this.DateViewed; pageView.PersonAliasId = this.PersonAliasId; pageView.PageTitle = this.PageTitle.Left(500); pageView.PageViewSessionId = pageViewSessionId.Value; rockContext.SaveChanges(); } }
/// <summary> /// Copies the base properties from a source PageViewUserAgent object /// </summary> /// <param name="source">The source.</param> public void CopyPropertiesFrom( PageViewUserAgent source ) { this.Id = source.Id; this.Browser = source.Browser; this.ClientType = source.ClientType; this.ForeignGuid = source.ForeignGuid; this.ForeignKey = source.ForeignKey; this.OperatingSystem = source.OperatingSystem; this.UserAgent = source.UserAgent; this.Guid = source.Guid; this.ForeignId = source.ForeignId; }
/// <summary> /// Execute method to write transaction to the database. /// </summary> public void Execute() { using ( var rockContext = new RockContext() ) { PageViewService pageViewService = new PageViewService( rockContext ); PageViewUserAgentService pageViewUserAgentService = new PageViewUserAgentService( rockContext ); PageViewSessionService pageViewSessionService = new PageViewSessionService( rockContext ); var userAgent = ( this.UserAgent ?? string.Empty ).Trim(); if ( userAgent.Length > 450 ) { userAgent = userAgent.Substring( 0, 450 ); // trim super long useragents to fit in pageViewUserAgent.UserAgent } // get user agent info var clientType = PageViewUserAgent.GetClientType( userAgent ); Parser uaParser = Parser.GetDefault(); ClientInfo client = uaParser.Parse( userAgent ); var clientOs = client.OS.ToString(); var clientBrowser = client.UserAgent.ToString(); // lookup the pageViewUserAgent, and create it if it doesn't exist var pageViewUserAgent = pageViewUserAgentService.Queryable().Where( a => a.UserAgent == userAgent ).FirstOrDefault(); if ( pageViewUserAgent == null ) { pageViewUserAgent = new PageViewUserAgent(); pageViewUserAgent.UserAgent = userAgent; pageViewUserAgent.ClientType = clientType; pageViewUserAgent.OperatingSystem = clientOs; pageViewUserAgent.Browser = clientBrowser; pageViewUserAgentService.Add( pageViewUserAgent ); rockContext.SaveChanges(); } else { // check if the user agent properties need to be updated if ( clientType != pageViewUserAgent.ClientType || clientOs != pageViewUserAgent.OperatingSystem || clientBrowser != pageViewUserAgent.Browser ) { pageViewUserAgent.ClientType = clientType; pageViewUserAgent.OperatingSystem = clientOs; pageViewUserAgent.Browser = clientBrowser; rockContext.SaveChanges(); } } // lookup PageViewSession, and create it if it doesn't exist Guid sessionId = this.SessionId.AsGuid(); int? pageViewSessionId = pageViewSessionService.Queryable().Where( a => a.PageViewUserAgentId == pageViewUserAgent.Id && a.SessionId == sessionId && a.IpAddress == this.IPAddress ).Select( a => (int?)a.Id ).FirstOrDefault(); if ( !pageViewSessionId.HasValue ) { var pageViewSession = new PageViewSession(); pageViewSession.PageViewUserAgentId = pageViewUserAgent.Id; pageViewSession.SessionId = sessionId; pageViewSession.IpAddress = this.IPAddress; pageViewSessionService.Add( pageViewSession ); rockContext.SaveChanges(); pageViewSessionId = pageViewSession.Id; } PageView pageView = new PageView(); pageViewService.Add( pageView ); // obfuscate rock magic token Regex rgx = new Regex( @"rckipid=([^&]*)" ); string cleanUrl = rgx.Replace( this.Url, "rckipid=XXXXXXXXXXXXXXXXXXXXXXXXXXXX" ); pageView.PageId = this.PageId; pageView.SiteId = this.SiteId; pageView.Url = cleanUrl.Left( 500 ); pageView.DateTimeViewed = this.DateViewed; pageView.PersonAliasId = this.PersonAliasId; pageView.PageTitle = this.PageTitle.Left( 500 ); pageView.PageViewSessionId = pageViewSessionId.Value; rockContext.SaveChanges(); } }