Beispiel #1
0
        private static void UpdateForPublish(ref HtmlDocument document,
                                             CompileResourceRequest request,
                                             KEntity kentity,
                                             List <CompilerError> compileErrors,
                                             Dictionary <string, int> customVariables,
                                             GetProjectDetailsResponseModel projectDetails,
                                             GetPartialPagesDetailsResponseModel partialPages = null,
                                             List <KLanguageModel> componentEntities          = null,
                                             bool isCSRFPresent = false
                                             )
        {
            var htmlString = document.DocumentNode.OuterHtml.ToString();

            htmlString = htmlString.Replace("itemscope=\"\"", "itemscope");
            if (!string.IsNullOrEmpty(htmlString))
            {
                if (isCSRFPresent)
                {
                    htmlString = Constants.CSRFRegularExpression.Replace(htmlString, Constants.CSRFReplacementToken);
                }

                var lines = htmlString.Split('\n');

                var descendants = document.DocumentNode.Descendants();
                var kdlOb       = descendants.Where(s => s.Attributes[LanguageAttributes.KDL.GetDescription()] != null);
                //store temporary original KDL value before replacement of _system object
                var tempKDL = string.Empty;
                if (kdlOb != null && kdlOb.Any())
                {
                    tempKDL = kdlOb.FirstOrDefault().GetAttributeValue(LanguageAttributes.KDL.GetDescription(), null);
                }

                for (int i = 0; i < lines.Length; i++)
                {
                    ReplaceCustomVariables(customVariables, lines, i);
                    InjectPartialView(request, kentity, projectDetails, partialPages, lines, i);
                    ReplaceComponentEntities(projectDetails, componentEntities, lines, i);
                    SetObject(compileErrors, kentity, projectDetails, lines, i, request.ClassName);
                }
                htmlString = string.Join("\n", lines);
                document   = GetDocumentObject();
                document.LoadHtml(htmlString);

                //restore back the original k-dl attribute
                if (!string.IsNullOrEmpty(tempKDL))
                {
                    document.DocumentNode.Descendants().First(x => x.Attributes["k-dl"] != null).Attributes["k-dl"].Value = tempKDL;
                }
                htmlString = document.DocumentNode.OuterHtml;
            }
        }
Beispiel #2
0
        private static void HandleScriptInjection(HtmlDocument document,
                                                  CompileResourceRequest request,
                                                  KEntity kentity,
                                                  string rootUrl,
                                                  GetProjectDetailsResponseModel projectDetails, bool isCSRF)
        {
            HtmlNode body        = document.DocumentNode.SelectSingleNode("//body");
            var      descendants = document.DocumentNode.Descendants();
            var      kPayObject  = descendants.Where(s => s.Attributes[LanguageAttributes.KPayAmount.GetDescription()] != null);


            if (body != null)
            {
                if (kPayObject != null && kPayObject.Any())
                {
                    HtmlNode kPayScript = document.CreateElement("script");
                    kPayScript.SetAttributeValue("src", Constants.KPayJsLink);
                    kPayScript.SetAttributeValue("type", "text/javascript");
                    kPayScript.SetAttributeValue("charset", "utf-8");
                    kPayScript.SetAttributeValue("defer", "defer");
                    kPayScript.SetAttributeValue("async", "async");
                    body.AppendChild(kPayScript);
                }

                if (isCSRF)
                {
                    HtmlNode csrfScript = document.CreateElement("script");
                    csrfScript.SetAttributeValue("src", Constants.CSRFTokenJSLink);
                    csrfScript.SetAttributeValue("type", "text/javascript");
                    csrfScript.SetAttributeValue("charset", "utf-8");
                    csrfScript.SetAttributeValue("defer", "defer");
                    csrfScript.SetAttributeValue("async", "async");
                    body.AppendChild(csrfScript);
                }

                HtmlNode ThemeIdWidget = HtmlNode.CreateNode(string.Format("<span style='display: none;' id='GET-VALUE(THEME-ID)'>{0}</span>", request.ProjectId));
                HtmlNode WebsiteId     = HtmlNode.CreateNode("<span style='display: none;' id='GET-VALUE(WEBSITE-ID)'>[KITSUNE_WEBSITE_ID]</span>");
                body.AppendChild(ThemeIdWidget);
                body.AppendChild(WebsiteId);

                if (kentity.EntityName != Constants.KPayDefaultSchema)
                {
                    HtmlNode RootAlias = HtmlNode.CreateNode(string.Format("<a style='display: none;' href='[[{0}]]' id='GET-URL(HOME)'></a>", rootUrl));
                    body.AppendChild(RootAlias);
                }
            }
        }
Beispiel #3
0
        private static List <KLanguageModel> UpdateModuleReference(GetProjectDetailsResponseModel projectDetails)
        {
            //List<Kitsune.Models.Project.KitsuneProject> modulesDetails = null;
            //  var api = new CompilerAPI();

            //if (projectDetails.Modules != null && projectDetails.Modules.Any())
            //{
            //    modulesDetails = new List<Kitsune.Models.Project.KitsuneProject>();
            //    var moduleDetail = new Kitsune.Models.Project.KitsuneProject();
            //    foreach (var module in projectDetails.Modules)
            //    {
            //        moduleDetail = api.GetProjectDetailsByClientIdApi(ClientIdConstants._defaultClientId, module.ProjectId);

            //        if (moduleDetail == null)
            //            return new CompileResult
            //            {
            //                Success = false,
            //                ErrorMessages = new List<CompilerError> { new CompilerError { Message = $"Module {module.ProjectId} not found." } }
            //            };
            //        modulesDetails.Add(moduleDetail);
            //    }
            //}
            var languageEntity = new List <KLanguageModel>();

            if (projectDetails != null && projectDetails.Components != null && projectDetails.Components.Any())
            {
                foreach (var component in projectDetails.Components.Where(x => x.SchemaId != null))
                {
                    var moduleEntity = new LanguageAPI().GetLanguageByClientId(component.SchemaId, ClientIdConstants._defaultClientId);

                    if (moduleEntity != null)
                    {
                        languageEntity.Add(new KLanguageModel {
                            _id = component.SchemaId, Entity = moduleEntity
                        });
                    }
                    else
                    {
                        ///TODO : Could not find module/app entity, Log/Inform user
                    }
                }
                //Add appClass as base class which will have reference of all reference app
                return(languageEntity.Any() ? languageEntity : null);
            }
            return(null);
        }
Beispiel #4
0
 private static void ReplaceComponentEntities(GetProjectDetailsResponseModel projectDetails, List <KLanguageModel> componentEntities, string[] lines, int i)
 {
     if (componentEntities != null && componentEntities.Any())
     {
         foreach (var component in componentEntities)
         {
             var matches = Constants.GeCustomVariableRegex($@"{component.Entity.EntityName}\.").Matches(lines[i]);
             if (matches != null && matches.Count > 0)
             {
                 foreach (var match in matches.ToList())
                 {
                     if (!string.IsNullOrEmpty(match.Value))
                     {
                         lines[i] = lines[i].Replace(match.Value, Constants.GetCustomVariableReplacementRegex($@"{component.Entity.EntityName}\.").Replace(match.Value, $"$1_system.components._{component._id}."));
                     }
                 }
             }
         }
     }
 }
        public KEntity GetKitsuneLanguage(string userEmail, string projectId, GetProjectDetailsResponseModel projectDetails, KEntity language, string userId)
        {
            if (language == null)
            {
                language = new KEntity()
                {
                    Classes = new List <KClass>(), EntityName = "_system"
                }
            }
            ;
            if (projectDetails != null && language != null)
            {
                var entityTemp = language;

                UpdateClassViews(userEmail, projectDetails, entityTemp);

                #region WebAction
                var webActions         = new WebActionAPI().GetWebActionList(userId).Result;
                var webActionBaseClass = new KClass
                {
                    ClassType   = KClassType.BaseClass,
                    Description = "Webaction",
                    Name        = "webactions",
                };
                IList <KProperty> PropertyList       = new List <KProperty>();
                IList <KClass>    webactionClassList = new List <KClass>();

                KClass webactionClass;
                if (webActions != null && webActions.WebActions != null)
                {
                    foreach (var webaction in webActions.WebActions)
                    {
                        //Webactions.webactionname
                        PropertyList.Add(new KProperty
                        {
                            DataType    = new DataType("wa" + webaction.Name.ToLower()),
                            Description = webaction.Name,
                            Name        = webaction.Name.ToLower(),
                            Type        = PropertyType.array
                        });

                        //Datatype (class) of the specific webaction
                        webactionClass = new KClass
                        {
                            ClassType    = KClassType.UserDefinedClass,
                            Description  = "Webaction products",
                            Name         = "wa" + webaction.Name.ToLower(),
                            PropertyList = webaction.Properties.Select(x => new KProperty
                            {
                                DataType    = new DataType(x.DataType),
                                Description = x.DisplayName,
                                Name        = x.PropertyName.ToLower(),
                                Type        = ConvertProperty(x.PropertyType),
                                IsRequired  = x.IsRequired
                            }).ToList()
                        };
                        webactionClass.PropertyList.Add(new KProperty
                        {
                            DataType    = new DataType("STR"),
                            Description = "Id",
                            Name        = "_id",
                            Type        = PropertyType.str
                        });
                        webactionClass.PropertyList.Add(new KProperty
                        {
                            DataType    = new DataType("DATE"),
                            Description = "Created on",
                            Name        = "createdon",
                            Type        = PropertyType.date
                        });
                        webactionClass.PropertyList.Add(new KProperty
                        {
                            DataType    = new DataType("DATE"),
                            Description = "Updated on",
                            Name        = "updatedon",
                            Type        = PropertyType.date
                        });
                        entityTemp.Classes.Add(webactionClass);
                    }
                }
                webActionBaseClass.PropertyList = PropertyList;
                entityTemp.Classes.Add(webActionBaseClass);
                #endregion

                if (projectId == projectDetails.ProjectId)
                {
                    language = entityTemp;
                }
            }
            return(language);
        }
 static void UpdateClassViews(string userEmail, GetProjectDetailsResponseModel projectDetails, KEntity entity)
 {
     if (projectDetails != null)
     {
         foreach (var resource in projectDetails.Resources.Where(x => x.IsStatic == false && !string.IsNullOrEmpty(x.ClassName)))
         {
             entity.Classes.Add(new KClass
             {
                 ClassType    = KClassType.BaseClass,
                 Description  = resource.SourcePath.ToLower(),
                 Name         = resource.ClassName,
                 PropertyList = new List <KProperty>()
                 {
                     new KProperty
                     {
                         DataType    = new DataType("FUNCTION"),
                         Description = "Page link",
                         Name        = "GETURL",
                         Type        = PropertyType.function
                     },
                     new KProperty
                     {
                         DataType    = new DataType("FUNCTION"),
                         Description = "Set details page object",
                         Name        = "SETOBJECT",
                         Type        = PropertyType.function
                     },
                     new KProperty
                     {
                         DataType    = new DataType("LINK"),
                         Description = "First page link",
                         Name        = "FIRSTPAGE",
                         Type        = PropertyType.obj
                     },
                     new KProperty
                     {
                         DataType    = new DataType("LINK"),
                         Description = "Last page link",
                         Name        = "LASTPAGE",
                         Type        = PropertyType.obj
                     },
                     new KProperty
                     {
                         DataType    = new DataType("STR"),
                         Description = "Current page number",
                         Name        = "CURRENTPAGENUMBER",
                         Type        = PropertyType.str
                     },
                     new KProperty
                     {
                         DataType    = new DataType("LINK"),
                         Description = "Next page link",
                         Name        = "NEXTPAGE",
                         Type        = PropertyType.obj
                     },
                     new KProperty
                     {
                         DataType    = new DataType("STR"),
                         Description = "Offset of list page",
                         Name        = "OFFSET",
                         Type        = PropertyType.str
                     },
                     new KProperty
                     {
                         DataType    = new DataType("LINK"),
                         Description = "Previous page link",
                         Name        = "PREVIOUSPAGE",
                         Type        = PropertyType.obj
                     },
                     new KProperty
                     {
                         DataType    = new DataType("STR"),
                         Description = "Set object for the details page",
                         Name        = "SETOBJECT",
                         Type        = PropertyType.function
                     },
                 }
             });
         }
     }
 }
        /// <summary>
        /// Validate k-tags and expressions in the document.
        /// </summary>
        /// <param name="document">Incoming document</param>
        /// <param name="request">Incoming request</param>
        /// <param name="compileErrors">list of errors during compile</param>
        /// <param name="kentity">project language</param>
        /// <param name="customVariables">custom variables found during compilation</param>
        /// <param name="rootUrl">root url</param>
        /// <param name="filePath">file path</param>
        public void ValidateDocument(HtmlDocument document, CompileResourceRequest request, List <CompilerError> compileErrors, KEntity kentity, Dictionary <string, int> customVariables, string rootUrl, string filePath, GetProjectDetailsResponseModel projectDetails, List <KEntity> componentsEntity = null)
        {
            Dictionary <string, bool>   objectNamesValidated  = new Dictionary <string, bool>();
            Dictionary <string, string> classNameAlias        = new Dictionary <string, string>();
            Dictionary <int, string>    classNameAliasLevel   = new Dictionary <int, string>();
            List <MatchNode>            objectNamesToValidate = new List <MatchNode>();

            entities.Add(kentity.EntityName, kentity);
            defaultEntity = kentity.EntityName;
            if (componentsEntity != null && componentsEntity.Any())
            {
                foreach (var comEntity in componentsEntity)
                {
                    if (!entities.Keys.Contains(comEntity.EntityName))
                    {
                        entities.Add(comEntity.EntityName, comEntity);
                    }
                }
            }

            var baseClassList = entities.SelectMany(x => x.Value?.Classes?.Where(y => y.ClassType == KClassType.BaseClass)?.ToList());

            HtmlNode      node = document.DocumentNode;
            List <string> dynamicTagDescriptors = GetDynamicTagDescriptors(typeof(LanguageAttributes));
            int           level = 0;

            while (node != null)
            {
                if (node.NodeType == HtmlNodeType.Element)
                {
                    var dynamicAttributes = node.Attributes.Where(x => dynamicTagDescriptors.Contains(x.Name.ToLower()));
                    for (int i = 0; i < dynamicAttributes.Count(); i++)
                    {
                        if (!string.IsNullOrEmpty(dynamicAttributes.ElementAt(i).Value))
                        {
                            Processor processor = ProcessorFactory.GetProcessor(dynamicAttributes.ElementAt(i).Name);
                            processor.ProcessNode(request, compileErrors, customVariables, rootUrl, filePath, classNameAlias, classNameAliasLevel, level, node, dynamicAttributes.ElementAt(i), objectNamesToValidate, this);
                        }
                        //k-partial k-norepeat attribute can be empty
                        else if (dynamicAttributes.ElementAt(i).Name?.ToLower() != LanguageAttributes.KPartial.GetDescription()?.ToLower() &&
                                 dynamicAttributes.ElementAt(i).Name?.ToLower() != LanguageAttributes.KNoRepeat.GetDescription()?.ToLower())
                        {
                            compileErrors.Add(CompileResultHelper.GetCompileError(string.Format(ErrorCodeConstants.InvalidKitsuneTagValue,
                                                                                                dynamicAttributes.ElementAt(i).Name,
                                                                                                dynamicAttributes.ElementAt(i).Line, dynamicAttributes.ElementAt(i).LinePosition)));
                        }
                    }
                    if (node.Name.Equals(LanguageAttributes.KScript.GetDescription(), StringComparison.InvariantCultureIgnoreCase))
                    {
                        Processor processor = ProcessorFactory.GetProcessor(node.Name.ToLower());
                        processor.ProcessNode(request, compileErrors, customVariables, rootUrl, filePath, classNameAlias, classNameAliasLevel, level, node, null, objectNamesToValidate, this);
                    }
                }

                if (node.HasChildNodes)
                {
                    node = node.FirstChild;
                    level++;
                }
                else
                {
                    ValidateExpressions(compileErrors, entities, objectNamesValidated, classNameAlias, baseClassList, node, objectNamesToValidate, customVariables, projectDetails);
                    if (node.NextSibling != null)
                    {
                        node = node.NextSibling;
                        ClearNameAliases(classNameAlias, classNameAliasLevel, level);
                    }
                    else
                    {
                        while (node.ParentNode != null && node.ParentNode.NextSibling == null)
                        {
                            node = node.ParentNode;
                            ValidateExpressions(compileErrors, entities, objectNamesValidated, classNameAlias, baseClassList, node, objectNamesToValidate, customVariables, projectDetails);
                            level--;
                            ClearNameAliases(classNameAlias, classNameAliasLevel, level);
                        }
                        node = node?.ParentNode;
                        if (node != null)
                        {
                            ValidateExpressions(compileErrors, entities, objectNamesValidated, classNameAlias, baseClassList, node, objectNamesToValidate, customVariables, projectDetails);
                        }
                        node = node?.NextSibling;
                        level--;
                        ClearNameAliases(classNameAlias, classNameAliasLevel, level);
                    }
                }
            }
        }
        /// <summary>
        /// Validate objects used in expressions are valid.
        /// </summary>
        /// <param name="compileErrors"></param>
        /// <param name="kentity"></param>
        /// <param name="objectNamesValidated"></param>
        /// <param name="classNameAlias"></param>
        /// <param name="baseClassList"></param>
        /// <param name="node"></param>
        private void ValidateExpressions(List <CompilerError> compileErrors, Dictionary <string, KEntity> entities, Dictionary <string, bool> objectNamesValidated, Dictionary <string, string> classNameAlias, IEnumerable <KClass> baseClassList, HtmlNode node, List <MatchNode> objectNamesToValidate, Dictionary <string, int> customVariables, GetProjectDetailsResponseModel projectDetails)
        {
            if (node.NodeType != HtmlNodeType.Element)
            {
                return;
            }
            int           lineNo           = 0;
            CompilerError viewExist        = null;
            CompilerError partialViewExist = null;
            string        viewExpression   = string.Empty;

            foreach (string line in node.OuterHtml.Split("\n"))
            {
                List <MatchNode> expressionNodes = HtmlHelper.GetExpressionFromElement(line, node.Line + lineNo);
                foreach (var expression in expressionNodes)
                {
                    try
                    {
                        viewExpression = expression.Value;
                        //View() function validation

                        viewExist = KitsuneCompiler.ValidateView(ref viewExpression, entities.First().Value);
                        if (viewExist != null)
                        {
                            viewExist.LineNumber   = expression.Line;
                            viewExist.LinePosition = expression.Column;
                            compileErrors.Add(viewExist);
                        }
                        //Partial() view function validation
                        partialViewExist = KitsuneCompiler.ValidatePartialView(ref viewExpression, projectDetails.Resources);
                        if (partialViewExist != null)
                        {
                            partialViewExist.LineNumber   = expression.Line;
                            partialViewExist.LinePosition = expression.Column;
                            compileErrors.Add(partialViewExist);
                        }

                        foreach (string raw_obj in Parser.GetObjects(viewExpression))
                        {
                            string obj = raw_obj.Replace("[[", "").Replace("]]", "").ToLower();
                            if (customVariables.ContainsKey(obj))
                            {
                                continue;
                            }
                            else
                            {
                                objectNamesToValidate.Add(new MatchNode {
                                    Value = obj, Line = node.Line + lineNo, Column = line.IndexOf(obj)
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        compileErrors.Add(CompileResultHelper.GetCompileError(ex.Message, node.Line, node.LinePosition));
                        continue;
                    }
                }
                lineNo++;
            }
            foreach (MatchNode obj in objectNamesToValidate)
            {
                Boolean isAlias    = false;
                string  objectName = obj.Value.ToLower();
                if ((classNameAlias.ContainsKey(objectName) && classNameAlias[objectName] == "") || customVariables.ContainsKey(objectName))
                {
                    continue;
                }
                string baseClassName = objectName.Split('.')[0];
                while (baseClassName != "kresult" && baseClassName != "search" && classNameAlias.ContainsKey(baseClassName))
                {
                    int index = objectName.IndexOf('.');
                    objectName    = classNameAlias[baseClassName] + (index >= 0 ? objectName.Substring(index) : "");
                    isAlias       = true;
                    baseClassName = objectName.Split('.')[0];
                }
                if (objectNamesValidated.ContainsKey(objectName))
                {
                    if (!objectNamesValidated[objectName] && !isAlias)
                    {
                        compileErrors.Add(CompileResultHelper.GetCompileError(String.Format(ErrorCodeConstants.UnrecognizedType, objectName), obj.Line, obj.Column));
                    }
                }
                else
                {
                    string[] objectPathArray  = objectName.ToLower().Split('.');
                    string   basePropertyName = objectPathArray[0];
                    var      baseClass        = baseClassList.FirstOrDefault(x => x.Name?.ToLower() == basePropertyName);

                    if (baseClass != null)
                    {
                        var baseEntity = entities.Keys.Contains(basePropertyName) ? entities[basePropertyName] : null;

                        if (baseEntity == null)
                        {
                            baseEntity = entities.First().Value;
                        }

                        IEnumerable <CompilerError> errorList = ValidateExpression(objectPathArray, 1, basePropertyName, PropertyType.obj, obj.Line, obj.Column, baseEntity);
                        if (errorList.Count() > 0)
                        {
                            compileErrors.AddRange(errorList);
                            objectNamesValidated.Add(objectName, false);
                        }
                        else
                        {
                            objectNamesValidated.Add(objectName, true);
                        }
                    }
                    else if (!ValidObjects.Contains(basePropertyName.Trim('[').Trim(']')) && ((basePropertyName.StartsWith("kresult") && !classNameAlias.ContainsKey("kresult")) || !basePropertyName.StartsWith("kresult")))
                    {
                        compileErrors.Add(CompileResultHelper.GetCompileError(String.Format(ErrorCodeConstants.UnrecognizedClass, basePropertyName), obj.Line, obj.Column));
                        //objectNamesValidated.Add(objectName, false);
                    }
                }
            }
            //InnerText returns text from inner html tags, need to clear so we don't run parser again. DOM is unaffected by this.
            node.InnerHtml = "";
            objectNamesToValidate.Clear();
        }
        public static ResourceMetaInfo MetaInfo(CompileResourceRequest req, string metadocument, GetProjectDetailsResponseModel projectDetails,
                                                KEntity kentity, List <string> tagsToIgnore)
        {
            try
            {
                if (tagsToIgnore == null)
                {
                    tagsToIgnore = new List <string>();
                }
                MetaInfoList = new List <SinglePositionProperty> {
                };
                metaList     = new List <Kitsune.Helper.MatchNode>();
                var documentRef = new HtmlDocument();
                documentRef = GetDocumentObject();
                if (!string.IsNullOrEmpty(metadocument))
                {
                    var document = GetDocumentObject();
                    try
                    {
                        document.LoadHtml(metadocument);
                    }
                    catch { }
                    documentRef = document;

                    var descendants = document.DocumentNode.Descendants();

                    if (!(req.IsStatic))
                    {
                        #region K-RepeatHandler
                        var repeatNodes = descendants.Where(x => x.Attributes[LanguageAttributes.KRepeat.GetDescription()] != null && !string.IsNullOrEmpty(x.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value));
                        if (repeatNodes != null && repeatNodes.Any())
                        {
                            var           krepeatCount = 0;
                            var           kval         = "";
                            string        match;
                            List <string> repeatForEachParam;
                            foreach (var node in repeatNodes)
                            {
                                node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value = string.Concat("[", node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value.Trim(), "]");
                                kval  = node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value;
                                match = Regex.Match(kval, Kitsune.Helper.Constants.WidgetRegulerExpression.ToString())?.Value?.Trim('[', ']');
                                if (string.IsNullOrEmpty(match))
                                {
                                }
                                //nfUXErrors.Add(new CompilerError { Message = "Invalid k-repeat value", LineNumber = node.Line, LinePosition = node.LinePosition });
                                else
                                {
                                    //if (match.IndexOf(" in ") > 0)
                                    //{
                                    //    repeatForEachParam = match.Split(new string[] { " in " }, StringSplitOptions.None)?.ToList();
                                    //    ///TODO : check condition for all params
                                    //    if (repeatForEachParam.Count == 2)
                                    //    {
                                    //        var regexes = Kitsune.Helper.Constants.WidgetRegulerExpression;
                                    //        var regexmatchies = regexes.Matches(node.InnerHtml);
                                    //        var regex = new Regex(MakeRegexEligible(krepeatvar[krepeatCount].Trim()));
                                    //        krepeatCount++;
                                    //        if (regexmatchies != null && regexmatchies.Count > 0)
                                    //            for (int i = 0; i < regexmatchies.Count; i++)
                                    //            {
                                    //                node.InnerHtml = node.InnerHtml.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[x]"));
                                    //            }

                                    //        foreach (var attr in node.Attributes.Where(x => x.Name.ToLower() != "k-repeat"))
                                    //        {
                                    //            regexmatchies = regexes.Matches(attr.Value);
                                    //            for (int i = 0; i < regexmatchies.Count; i++)
                                    //            {
                                    //                attr.Value = attr.Value.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[x]"));
                                    //            }
                                    //        }
                                    //    }
                                    //}
                                    //else
                                    //{
                                    repeatForEachParam = match.Split(',').ToList();
                                    if (repeatForEachParam.Count == 3)
                                    {
                                        var repeatCount = repeatForEachParam[2].Split(':');
                                        tagsToIgnore.Add(repeatForEachParam[0].Trim());
                                        tagsToIgnore.Add(repeatForEachParam[1].Trim());
                                        if (repeatCount[0].Contains("offset"))
                                        {
                                            repeatCount[0] = "x";
                                        }
                                        var regexes       = Kitsune.Helper.Constants.WidgetRegulerExpression;
                                        var regexmatchies = regexes.Matches(node.InnerHtml);
                                        var regex         = new Regex(MakeRegexEligible(@"[\s*" + repeatForEachParam[1].Trim() + @"\s*]"));
                                        if (regexmatchies != null && regexmatchies.Count > 0)
                                        {
                                            for (int i = 0; i < regexmatchies.Count; i++)
                                            {
                                                if (repeatCount[1].Contains("length()"))
                                                {
                                                    node.InnerHtml = node.InnerHtml.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[x]"));
                                                }
                                                else
                                                {
                                                    node.InnerHtml = node.InnerHtml.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "[" + repeatCount[0].Trim() + "x" + repeatCount[1].Trim() + "]"));
                                                }
                                            }
                                        }

                                        foreach (var attr in node.Attributes.Where(x => x.Name.ToLower() != "k-repeat"))
                                        {
                                            regexmatchies = regexes.Matches(attr.Value);
                                            for (int i = 0; i < regexmatchies.Count; i++)
                                            {
                                                attr.Value = attr.Value.Replace(regexmatchies[i].Value, regex.Replace(regexmatchies[i].Value, "$1[" + repeatCount[0].Trim() + "x" + repeatCount[1].Trim() + "]$3$4"));
                                            }
                                        }
                                    }

                                    //}
                                }
                            }
                            foreach (var node in repeatNodes)
                            {
                                node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value = node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value.Substring(1, node.Attributes[LanguageAttributes.KRepeat.GetDescription()].Value.Length - 2);
                            }
                            var newHtml = document.DocumentNode.OuterHtml;
                            document = GetDocumentObject();
                            document.LoadHtml(newHtml);
                            descendants = document.DocumentNode.Descendants();

                            #endregion
                        }


                        string[] NfWidgets = document.DocumentNode.OuterHtml.Split('\n');
                        ExtractProperty(NfWidgets, req.UserEmail, req.ProjectId, tagsToIgnore.Distinct().ToList());

                        var metaObject = new List <PropertyDetails>();

                        //MetaInfoListString.Sort();

                        List <MultiplePositionProperty> distinctInput = MetaInfoList.GroupBy(i => i.Property)
                                                                        .Select(g => new MultiplePositionProperty
                        {
                            Property  = g.Key,
                            Positions = g.Select(i => i.Position).ToList()
                        }).ToList();
                        distinctInput = distinctInput.GroupBy(i => i.Property)
                                        .Select(g => new MultiplePositionProperty
                        {
                            Property  = g.Key,
                            Positions = g.SelectMany(i => i.Positions).ToList()
                        }).ToList();

                        //# For writing unmerged properties to file in local
                        //List<string> MetaInfoListString = new List<string>();
                        //foreach (var metainfo in distinctInput)
                        //{
                        //    List<string> write = new List<string> { metainfo.Property };
                        //    foreach (var position in metainfo.Positions)
                        //    {
                        //        write.Add(position.Column.ToString());
                        //        write.Add(position.Line.ToString());
                        //    }
                        //    MetaInfoListString.Add(string.Join(",", write));
                        //}
                        //MetaInfoListString.Sort();
                        //string outputFile = @"D:\" + req.UserEmail + @"\" + req.SourcePath.Replace("/", "");
                        //System.IO.File.WriteAllLines(outputFile + @".csv", MetaInfoListString);

                        var metaInfoMergeList = distinctInput.Where(x => x.Property.Contains('[')).ToList();
                        distinctInput.RemoveAll(x => x.Property.Contains('['));
                        var mergedMetaInfoList = ksegregation.MergeProperty(metaInfoMergeList);
                        distinctInput.AddRange(mergedMetaInfoList);
                        distinctInput = distinctInput.OrderBy(x => x.Property).ToList();

                        //# For writing merged properties to file in local
                        //List<string> MetaInfoListString = new List<string>();
                        //foreach (var metainfo in distinctInput)
                        //{
                        //    List<string> write = new List<string> { metainfo.Property };
                        //    foreach (var position in metainfo.Positions)
                        //    {
                        //        write.Add(position.Column.ToString());
                        //        write.Add(position.Line.ToString());
                        //    }
                        //    MetaInfoListString.Add(string.Join(",", write));
                        //}
                        //string optimizedOutputFile = @"D:\optimized_" + req.UserEmail + @"\" + req.SourcePath.Replace("/", "");
                        //System.IO.File.WriteAllLines(optimizedOutputFile + @".csv", MetaInfoListString);


                        var metaClassBuilder = new MetaClassBuilder();
                        var metaClass        = metaClassBuilder.BuildMetaClass(kentity, distinctInput, req.SourcePath);

                        return(new ResourceMetaInfo
                        {
                            IsError = false,
                            Property = distinctInput,
                            metaClass = metaClass
                        });
                    }

                    return(new ResourceMetaInfo
                    {
                        IsError = true,
                        Message = "Input should not be Static"
                    });
                }
                return(new ResourceMetaInfo
                {
                    IsError = true,
                    Message = "Input should not be empty"
                });
            }
            catch (Exception ex)
            {
                //TODO: throw error
                //throw new Exception(string.Format("Excepiton occured while generating metainfo: {0}", ex.Message));
                return(new ResourceMetaInfo
                {
                    IsError = true,
                    Message = string.Format("Excepiton occured while generating metainfo: {0}", ex.Message)
                });
            }
        }
        public async Task <ProjectPreview> GetPreviewAsync(string userEmail, string projectId, GetResourceDetailsResponseModel resourceDetails,
                                                           GetProjectDetailsResponseModel projectDetails,
                                                           KEntity languageEntity,
                                                           GetPartialPagesDetailsResponseModel partialPages,
                                                           string fpTag, string userId)
        {
            var    watch        = new Stopwatch();
            string compilerTime = "";

            watch.Start();

            ProjectPreview result = null;

            if (projectId != null && resourceDetails != null)
            {
                var doc = new HtmlAgilityPack.HtmlDocument();

                var compileResult = KitsuneCompiler.CompileHTML(new CompileResourceRequest
                {
                    UserEmail   = userEmail,
                    FileContent = resourceDetails.HtmlSourceString,
                    IsDev       = true,
                    IsPublish   = true,
                    ProjectId   = projectId,
                    SourcePath  = resourceDetails.SourcePath,
                    ClassName   = resourceDetails.ClassName,
                    IsPreview   = true
                }, out doc, projectDetails, languageEntity, partialPages);
                compilerTime += ", CompileHTML : " + watch.ElapsedMilliseconds.ToString();

                watch.Restart();
                var previewRes = "";
                if (!compileResult.Success)
                {
                    previewRes = ErrorCodeConstants.CompilationError;
                }
                else if (resourceDetails.IsStatic)
                {
                    previewRes = compileResult.CompiledString;
                }
                else
                {
                    try
                    {
                        var client = new HttpClient();
                        client.BaseAddress = new Uri("http://kitsune-demo-identifier-952747768.ap-south-1.elb.amazonaws.com");
                        byte[] bytes  = Encoding.UTF8.GetBytes(compileResult.CompiledString);
                        string base64 = Convert.ToBase64String(bytes);
                        var    obData = new KitsunePreviewModel
                        {
                            DeveloperId       = userId,
                            FileContent       = base64,
                            NoCacheQueryParam = null,
                            ProjectId         = projectId,
                            View       = resourceDetails.SourcePath,
                            WebsiteTag = fpTag
                        };
                        var jsonData = JsonConvert.SerializeObject(obData);

                        var response = await client.PostAsync("/home/kpreview", new StringContent(jsonData, Encoding.UTF8, "application/json"));

                        if (!((int)response.StatusCode >= 200 && (int)response.StatusCode <= 300))//If status code is not 20X, error.
                        {
                            // Log(response?.Content?.ReadAsStringAsync()?.Result);
                            throw new Exception(ErrorCodeConstants.UnableToGetPreview);
                        }
                        previewRes = response.Content.ReadAsStringAsync().Result;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                var previewTime = watch.ElapsedMilliseconds;
                result = new ProjectPreview
                {
                    HtmlString   = previewRes,
                    CompilerTime = compilerTime,
                    PreviewTime  = previewTime
                };
            }
            return(result);
        }
Beispiel #11
0
        private static void InjectPartialView(CompileResourceRequest request, KEntity kentity, GetProjectDetailsResponseModel projectDetails, GetPartialPagesDetailsResponseModel partialPages, string[] lines, int i)
        {
            var partialregex  = Constants.PartialPageRegularExpression;
            var partialresult = partialregex.Matches(lines[i]);

            if (partialPages != null && partialPages.Resources != null && partialPages.Resources.Any())
            {
                for (int mc = 0; mc < partialresult.Count; mc++)
                {
                    if (partialresult[mc].Groups.Count > 1)
                    {
                        var groupparam = partialresult[mc].Groups[1].Value?.Trim('(', ')').Trim('\'').ToLower();
                        if (!string.IsNullOrEmpty(groupparam))
                        {
                            var partialViewParams = groupparam.Split(',');
                            var viewName          = partialViewParams[0]?.Trim('\'');

                            if (projectDetails.Resources.Any(x => x.PageType == Models.Project.KitsunePageType.PARTIAL && x.SourcePath.ToLower() == viewName.ToLower()))
                            {
                                var htmlData = new HtmlDocument();
                                htmlData.LoadHtml(partialPages.Resources.FirstOrDefault(x => string.Compare(x.SourcePath, viewName, true) == 0).HtmlSourceString);
                                //Support partial page with Html body and without body tag
                                string partialbody       = htmlData.DocumentNode.SelectSingleNode("/html/body")?.InnerHtml ?? htmlData.DocumentNode.OuterHtml;
                                var    partialCompiledOb = KitsuneCompiler.CompileHTML(new CompileResourceRequest
                                {
                                    FileContent = partialbody,
                                    IsPublish   = true,
                                    SourcePath  = viewName,
                                    PageType    = Models.Project.KitsunePageType.PARTIAL,
                                    ProjectId   = request.ProjectId,
                                    UserEmail   = request.UserEmail
                                }, out htmlData, projectDetails, kentity, partialPages);
                                if (partialCompiledOb != null && partialCompiledOb.Success)
                                {
                                    lines[i] = lines[i].Replace(partialresult[mc].Value, partialCompiledOb.CompiledString);
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #12
0
        public static CompileResult CompileHTML(CompileResourceRequest req, out HtmlDocument documentRef,
                                                GetProjectDetailsResponseModel projectDetails,
                                                KEntity kentity,
                                                GetPartialPagesDetailsResponseModel partialPages = null, bool subsequentCompilation = false, List <KLanguageModel> componentEntities = null) // partialPage is required only for the publish/preview
        {
            ///TODO : Handle the kitsune-settings.xml saperatly
            documentRef = GetDocumentObject();
            var filePath = req.SourcePath.Trim('/');

            if (!string.IsNullOrEmpty(req.FileContent))
            {
                HtmlDocument             document;
                List <CompilerError>     nfUXErrors      = new List <CompilerError>();
                Dictionary <string, int> customVariables = new Dictionary <string, int>();
                string rootUrl;

                DocumentPreprocessor.PreprocessDocument(req, kentity, filePath, out document, nfUXErrors, out rootUrl);
                documentRef = document;

                /*
                 * if (document.ParseErrors.Count() > 0)
                 * {
                 *  return CompileResultHelper.GetCompileResult(nfUXErrors, req.SourcePath, false);
                 * }
                 */
                req.UrlPattern = req.SourcePath; //assigning default urlpattern

                if (!req.IsStatic)
                {
                    if (kentity == null)
                    {
                        return(CompileResultHelper.GetErrorCompileResult("Schema not found for this project.", req.SourcePath));
                    }
                    else if (kentity.EntityName == null)
                    {
                        kentity.EntityName = Constants.KPayDefaultSchema;
                    }

                    HtmlDocument documentClone = GetDocumentObject();
                    documentClone.LoadHtml(document.DocumentNode.OuterHtml);
                    DocumentValidator validator = new DocumentValidator();
                    validator.ValidateDocument(documentClone, req, nfUXErrors, kentity, customVariables, rootUrl, filePath, projectDetails, componentEntities?.Select(x => x.Entity)?.ToList());
                    if (nfUXErrors.Count == 0)
                    {
                        if (req.IsPublish)
                        {
                            var csrfPresent = Constants.CSRFRegularExpression.IsMatch(document.DocumentNode.OuterHtml);

                            UpdateForPublish(ref document, req, kentity, nfUXErrors, customVariables, projectDetails, partialPages, componentEntities, csrfPresent);

                            if (req.PageType != Models.Project.KitsunePageType.PARTIAL)
                            {
                                HandleScriptInjection(document, req, kentity, rootUrl, projectDetails, csrfPresent);
                            }
                        }
                    }

                    /*
                     * KitsunePage myPage = (new NewRootNodeProcessor()).Process(document.DocumentNode.OuterHtml);
                     * var jsonSerializeSettings = new JsonSerializerSettings();
                     * jsonSerializeSettings.TypeNameHandling = TypeNameHandling.Objects;
                     * string output = JsonConvert.SerializeObject(myPage, Formatting.None, jsonSerializeSettings);
                     * //KitsunePage myNewPage = JsonConvert.DeserializeObject<KitsunePage>(output, jsonSerializeSettings);
                     */
                }
                UpdatePathsForPreview(req, document);
                if (nfUXErrors.Count > 0)
                {
                    return new CompileResult {
                               Success = false, ErrorMessages = nfUXErrors, PageName = req.SourcePath
                    }
                }
                ;
                else
                {
                    return new CompileResult {
                               Success = true, CompiledString = document.DocumentNode.OuterHtml.ToString(), ErrorMessages = nfUXErrors, PageName = req.SourcePath, CustomVariables = customVariables
                    }
                };
            }

            return(new CompileResult
            {
                Success = false,
                ErrorMessages = new List <CompilerError> {
                    new CompilerError {
                        Message = "Input should not be empty"
                    }
                },
                PageName = req.SourcePath,
            });
        }
Beispiel #13
0
        private static void SetObject(List <CompilerError> compileErrors, KEntity kentity, GetProjectDetailsResponseModel projectDetails, string[] lines, int i, string currentClassName)
        {
            var pageClassResult = Helper.Constants.ViewClassRegularExpression.Matches(lines[i]);

            for (int j = 0; j < pageClassResult.Count; j++)
            {
                if (pageClassResult[j].Groups.Count > 1)
                {
                    var groupparam = pageClassResult[j].Groups[2].Value?.Trim('(', ')');
                    if (!string.IsNullOrEmpty(groupparam))
                    {
                        var partialViewParams = groupparam.Split(',');
                        var viewName          = partialViewParams[0]?.Trim('\'');

                        if (!kentity.Classes.Any(x => x.ClassType == KClassType.BaseClass && x.Description == viewName.ToLower() && x.Name != null))
                        {
                            compileErrors.Add(new CompilerError {
                                LineNumber = i + 1, LinePosition = 1, Message = string.Format("Page '{0}' dose not exist", viewName)
                            });
                        }
                        else
                        {
                            lines[i] = lines[i].Replace(pageClassResult[j].Groups[1].Value, kentity.Classes.First(x => x.ClassType == KClassType.BaseClass && x.Description != null && x.Description.ToLower() == viewName.ToLower() && x.Name != null).Name);
                            List <MatchNode> nodes = new List <MatchNode>();
                            ResourceItemMeta pageDetails;
                            nodes = HtmlHelper.SetObjectExpressionFromElement(lines[i], i);
                            if (nodes != null && nodes.Any())
                            {
                                foreach (var node in nodes)
                                {
                                    var    ob        = node.Value ?? "";
                                    string className = null;
                                    pageDetails = null;

                                    // foreach (var ob in languageObjects)
                                    if (ob.ToLower().Contains("geturl()"))
                                    {
                                        className   = ob.IndexOf('.') > 0 ? ob.Trim('[', ']').Substring(0, ob.Trim('[', ']').IndexOf('.')) : ob.Trim('[', ']');
                                        pageDetails = projectDetails.Resources.FirstOrDefault(x => x.ClassName == className.ToUpper());
                                        string pattern = pageDetails.UrlPattern;
                                        if (!currentClassName.Equals(className, StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            string[] segments = pattern.Split('/');
                                            for (int segIndex = 0; segIndex < segments.Length; segIndex++)
                                            {
                                                if (segments[segIndex].IndexOf("currentpagenumber", StringComparison.InvariantCultureIgnoreCase) > 0)
                                                {
                                                    segments[segIndex] = segments[segIndex].Replace(className + ".currentpagenumber", "'1'", StringComparison.InvariantCultureIgnoreCase);
                                                }
                                            }
                                            pattern = String.Join("/", segments);
                                        }
                                        if (pageDetails != null)
                                        {
                                            lines[i] = lines[i].Replace(ob, pattern);
                                        }
                                    }
                                    else if (ob.ToLower().Contains("setobject"))
                                    {
                                        className   = ob.IndexOf('.') > 0 ? ob.Trim('[', ']').Substring(0, ob.Trim('[', ']').IndexOf('.')) : ob.Trim('[', ']');
                                        pageDetails = projectDetails.Resources.FirstOrDefault(x => x.ClassName == className.ToUpper());
                                        if (pageDetails != null)
                                        {
                                            if (!string.IsNullOrEmpty(pageDetails.KObject))
                                            {
                                                var matches = Kitsune.Helper.Constants.FunctionParameterExpression.Match(ob);
                                                if (matches != null && matches.Groups?.Count > 0 && !string.IsNullOrEmpty(pageDetails.UrlPattern))
                                                {
                                                    var tempKObjectsValue = ((pageDetails.KObject.Split(':').Count() > 1) ? pageDetails.KObject.Split(':')[0] : pageDetails.KObject).Split(',');
                                                    var matchOBject       = matches.Groups[1].Value.Split(',');
                                                    var pattern           = pageDetails.UrlPattern;
                                                    var widgetNodeForKObj = Kitsune.Helper.Constants.WidgetRegulerExpression.Matches(pageDetails.UrlPattern);
                                                    if (matchOBject.Length == tempKObjectsValue.Length && widgetNodeForKObj != null && widgetNodeForKObj.Count > 0)
                                                    {
                                                        for (var mi = 0; mi < tempKObjectsValue.Length; mi++)
                                                        {
                                                            for (var wc = 0; wc < widgetNodeForKObj.Count; wc++)
                                                            {
                                                                pattern = pattern.Replace(widgetNodeForKObj[wc].Value, Kitsune.Helper.Constants.GetKObjectReplaceParamRegex(tempKObjectsValue[mi], true).Replace(widgetNodeForKObj[wc].Value, matchOBject[mi]));
                                                            }
                                                        }
                                                    }
                                                    //TODO : support for single object if only using one _kid
                                                    else if (widgetNodeForKObj != null && widgetNodeForKObj.Count > 0)
                                                    {
                                                        for (var mi = 0; mi < tempKObjectsValue.Length; mi++)
                                                        {
                                                            for (var wc = 0; wc < widgetNodeForKObj.Count; wc++)
                                                            {
                                                                pattern = pattern.Replace(widgetNodeForKObj[wc].Value, Kitsune.Helper.Constants.GetKObjectReplaceParamRegex(tempKObjectsValue[mi], true).Replace(widgetNodeForKObj[wc].Value, matchOBject[0]));
                                                            }
                                                        }
                                                    }
                                                    lines[i] = lines[i].Replace(ob, pattern);
                                                }
                                            }
                                            else
                                            {
                                                compileErrors.Add(new CompilerError
                                                {
                                                    LineNumber   = i,
                                                    LinePosition = lines[i].IndexOf("setobject", StringComparison.InvariantCultureIgnoreCase),
                                                    Message      = string.Format("K-Object not found for the page '{0}'", pageDetails.SourcePath)
                                                });
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        compileErrors.Add(new CompilerError {
                            LineNumber = i + 1, LinePosition = 1, Message = "Page view parameters missing."
                        });
                    }
                }
                else
                {
                    compileErrors.Add(new CompilerError {
                        LineNumber = i + 1, LinePosition = 1, Message = "Invalid page view syntax"
                    });
                }
            }
        }