Esempio n. 1
0
        public static string PatchArrayFunctions(string Code)
        {
            var tokens     = PhpParser.StripWhitespaceAndComments(new PhpVH.LexicalAnalysis.PhpLexer(Code).GetTokens().ToArray());
            var arrayFuncs = tokens
                             .Where((x, index) =>
                                    index > 1 &&
                                    x.TokenType == LexicalAnalysis.PhpTokenType.Identifier &&
                                    tokens[index - 1].TokenType != LexicalAnalysis.PhpTokenType.functionKeyword &&
                                    tokens[index - 2].TokenType != LexicalAnalysis.PhpTokenType.functionKeyword &&
                                    tokens[index - 1].TokenType != LexicalAnalysis.PhpTokenType.ObjectOperator &&
                                    tokens[index - 1].TokenType != LexicalAnalysis.PhpTokenType.ScopeResolutionOperator &&
                                    PhpName.ArrayFunctions.Contains(x.Lexeme))
                             .Reverse()
                             .ToArray();

            var c = new StringBuilder(Code);

            foreach (var f in arrayFuncs)
            {
                c.Insert(f.Index + f.Lexeme.Length, "_override");
            }

            return(c.ToString());
        }
        public void Set(DirectoryInfo Directory)
        {
            IEnumerable <FileInfo> files = null;

            try
            {
                files = Directory
                        .GetFiles("*", SearchOption.AllDirectories)
                        .Where(x =>
                               x.Extension.ToLower() == ".php" ||
                               x.Extension.ToLower() == ".inc");
            }
            catch (UnauthorizedAccessException)
            {
                var error = string.Format("\r\nError enumerating files in {0}", Directory.FullName);
                ScannerCli.DisplayError(error);
                return;
            }

            var progressBar = new CliProgressBar(files.Count());

            progressBar.Write();

            files.AsyncIter(f =>
            {
                if (!CreateBackup(f))
                {
                    return;
                }

                string php = File.ReadAllText(f.FullName);

                PhpToken[] tokens = null;

                Action getTokens                   = () =>
                                            tokens = PhpParser.StripWhitespaceAndComments(new PhpLexer(php)
                                                                                          .GetTokens()
                                                                                          .ToArray());

                getTokens();

                lock (Program.PageFieldTable)
                {
                    if (!Program.PageFieldTable.ContainsKey(f.FullName))
                    {
                        Program.PageFieldTable.Add(f.FullName, new Dictionary <string, List <string> >());
                    }

                    var superglobals = StaticAnalyzer.FindSuperglobalFields(tokens);

                    foreach (var superglobal in superglobals)
                    {
                        if (!Program.PageFieldTable[f.FullName].ContainsKey(superglobal.Key))
                        {
                            Program.PageFieldTable[f.FullName].Add(superglobal.Key, new List <string>());
                        }


                        var newValues = superglobal.Value
                                        .Where(x => !Program.PageFieldTable[f.FullName][superglobal.Key].Contains(x));

                        Program.PageFieldTable[f.FullName][superglobal.Key].AddRange(newValues);
                    }
                }

                lock (Program.FileIncludeTable)
                    Program.FileIncludeTable.Add(f.FullName,
                                                 PhpParser.GetIncludedFiles(tokens));

                var functions = PhpParser.GetGlobalFunctionCalls(tokens);

                if (Program.Config.CodeCoverageReport > 0)
                {
                    php = ScanMetrics.Default.Annotator.AnnotateCode(f.FullName, php, tokens);
                }

                if (!Program.Config.HookSuperglobals)
                {
                    php = PreloadHelper.Patch(php);
                }

                foreach (Hook h in this)
                {
                    h.Set(ref php);
                }

                getTokens();

                string include = "require_once('" +
                                 Program.Config.WebRoot.Replace('\\', '/') + "/" + Hook.HookFileName + "');";

                if (tokens.Length >= 2 && tokens[1].TokenType == PhpTokenType.namespaceKeyword)
                {
                    var eos = tokens.FirstOrDefault(x => x.TokenType == PhpTokenType.EndOfStatement);

                    php = php.Insert(eos.Index + 1, "\r\n" + include + "\r\n");
                }
                else
                {
                    php = "<?php " + include + " ?>" + php;
                }

                try
                {
                    File.WriteAllText(f.FullName, php);
                }
                catch (UnauthorizedAccessException ex)
                {
                    ScannerCli.DisplayError(string.Format("\r\n\r\nError hooking file\r\n{0}\r\n", ex.Message));

                    return;
                }
                progressBar.Value++;
                progressBar.Write();
            });
        }