Esempio n. 1
0
        // handles liquid template assets
        public ActionResult Handler(string filename, string domainpath)
        {
            string uniqueid;

            if (domainpath == "facebook")
            {
                uniqueid = domainpath;
            }
            else
            {
                var pathparts = domainpath.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
                uniqueid = pathparts[0];
            }

            // setup physical path first
            string liquidPhysicalPath;
            bool   isSVG = false;

            if (Request.RawUrl.IndexOf(".svg.png") != -1)
            {
                liquidPhysicalPath = Request.PhysicalPath.Replace(".png", ".liquid");
                isSVG = true;
            }
            else
            {
                liquidPhysicalPath = Request.PhysicalPath + ".liquid";
            }

            // cachekey = filename
            dynamic result;
            var     cachekey = ThemeHandler.GetCacheKey(uniqueid, filename, (bool)Session[BrowserCapability.IsMobileSession]);

            if (!CacheHelper.Instance.TryGetCache(CacheItemType.liquid_assets, cachekey, out result))
            {
                if (System.IO.File.Exists(liquidPhysicalPath))
                {
                    LiquidTemplateBase template;

                    var             accountHostname = Request.Headers["Host"];
                    MASTERsubdomain sd;
                    using (var db = new tradelrDataContext())
                    {
#if DEBUG
                        if (accountHostname.EndsWith("localhost"))
#else
                        if (accountHostname.EndsWith("tradelr.com"))
#endif
                        {
                            ////////////// handles case for subdomains
                            string[] host = accountHostname.Split('.');
                            // not on a subdomain

                            sd = db.GetSubDomain(host[0]);
                        }
                        else
                        {
                            ////////////////// handles case for custom subdomains
                            sd = db.GetCustomHostname(accountHostname);
                        }

                        template = new LiquidTemplateBase(sd,
                                                          (bool)Session[BrowserCapability.IsMobileSession]);

                        var parsed_string = template.ReadTemplateFile(liquidPhysicalPath);

                        template.InitContentTemplate(parsed_string);
                        template.AddParameters("shop", sd.ToLiquidModel());
                    }

                    var dirIndex = liquidPhysicalPath.LastIndexOf("\\");

                    // handle not found
                    var config_file = liquidPhysicalPath.Substring(0, dirIndex).Replace("assets", "config\\settings_data.json");
                    if (System.IO.File.Exists(config_file))
                    {
                        var current = template.ReadThemeSettings(config_file);
                        if (current != null)
                        {
                            template.AddParameters("settings", current);
                        }

                        if (isSVG)
                        {
                            // convert to png
                            using (var stream = template.RenderBasicToStreamNoHeader())
                            {
                                var svg = SvgDocument.Open(stream);
                                result = svg.Draw();
                            }
                        }
                        else
                        {
                            result = template.RenderBasicNoHeader();
                        }

                        CacheHelper.Instance.Insert(CacheItemType.liquid_assets, cachekey, result);
                        if (uniqueid != "facebook")
                        {
                            CacheHelper.Instance.add_dependency(DependencyType.liquid_assets, uniqueid, CacheItemType.liquid_assets, cachekey);
                        }
                    }
                }
            }

            if (Request.RawUrl.IndexOf(".css") != -1)
            {
                Response.ContentType = "text/css";
                return(Content(result));
            }

            if (Request.RawUrl.IndexOf(".js") != -1)
            {
                Response.ContentType = "application/x-javascript";
                return(Content(result));
            }

            if (isSVG)
            {
                return(new SvgToPngActionResult(result));
            }

            Syslog.Write(string.Format("Unknown filetype: {0}", Request.RawUrl));
            return(new EmptyResult());
        }
Esempio n. 2
0
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            accountHostname = Request.Headers["Host"];
            #if DEBUG
            if (accountHostname == "local")
            {
                filterContext.Result = new RedirectResult(GeneralConstants.HTTP_HOST + Request.Url.PathAndQuery);
                return;
            }
            #else
            if (accountHostname == null || accountHostname == "tradelr.com" || accountHostname == "98.126.29.28")
            {
                filterContext.Result = new RedirectResult(GeneralConstants.HTTP_HOST + Request.Url.PathAndQuery);
                return;
            }
            #endif
            ////////////////////// subdomain check ///////////////////////////////
            #if DEBUG
            if (accountHostname.EndsWith("localhost"))
            #else
            if (accountHostname.EndsWith("tradelr.com"))
            #endif
            {
                ////////////// handles case for subdomains
                string[] host = accountHostname.Split('.');
                string hostSegment = "";

                // not on a subdomain
                if (!Utility.IsOnSubdomain(host, out hostSegment))
                {
                    return;
                }

                MASTERdomain = db.GetSubDomain(hostSegment);

                // ensure that incoming host name matches x.tradelr.com.
                // this is to handle www.x.tradelr.com returning www.tradelr.com
                if (MASTERdomain != null)
                {
            #if DEBUG
                    var validHost = string.Format("{0}.localhost", hostSegment);
            #else
                    var validHost = string.Format("{0}.tradelr.com", hostSegment);
            #endif
                    if (validHost != accountHostname)
                    {
                        filterContext.Result = new RedirectResult(string.Format("{0}://{1}", Request.Url.Scheme, validHost));
                        return;
                    }
                }
            }
            else
            {
                ////////////////// handles case for custom subdomains
                MASTERdomain = db.GetCustomHostname(accountHostname);
            }

            if (MASTERdomain == null)
            {
                // subdomain does not exist
                filterContext.RequestContext.HttpContext.Response.StatusCode = HttpStatusCode.NotFound.ToInt();
                filterContext.Result = new RedirectResult(GeneralConstants.HTTP_HOST);
                return;
            }

            /////////// SUBDOMAIN EXISTS
            accountLimits = MASTERdomain.accountType.ToEnum<AccountPlanType>().ToAccountLimit();
            accountSubdomainName = MASTERdomain.name;
            subdomainid = MASTERdomain.id;
            stats = MASTERdomain.ToSubdomainStats();
            IsStoreEnabled = MASTERdomain.IsStoreEnabled();
            baseviewmodel.storeEnabled = IsStoreEnabled;
            subdomainFlags = (SubdomainFlags) MASTERdomain.flags;

            if ((MASTERdomain.flags & (int)SubdomainFlags.OFFLINE_ENABLED) != 0)
            {
                var browsertype = Request.Browser.Type.ToLower();
                if (browsertype.Contains("safari") || browsertype.Contains("chrome"))
                {
                    baseviewmodel.manifestFile = "manifest=\"/manifest\"";
                }
            }
            baseviewmodel.orgName = MASTERdomain.organisation.name;
            baseviewmodel.shopUrl = accountHostname;

            /////////////////////// session check ///////////////////////////////
            token = Request.RequestContext.HttpContext.Items["token"] as TradelrSecurityToken;

            if (token == null)
            {
                GetAuthCookie();
            }

            if (token != null)
            {
                Request.RequestContext.HttpContext.Items["token"] = token;
                sessionid = token.UserID;
                var usr = db.GetUserById(sessionid.Value, subdomainid.Value);
                if (usr != null)
                {
                    baseviewmodel.notifications = usr.ToNotification(MASTERdomain.trialExpired).ToJson();

                }

                role = token.UserRole.ToEnum<UserRole>();
                baseviewmodel.role = role;

                permission = token.Permission.ToEnum<UserPermission>();
                baseviewmodel.permission = permission;
            }

            base.OnActionExecuting(filterContext);
        }