public void Set(ref string PHP) { var lexer = new PhpLexer(PHP); var tokens = lexer.GetTokens().ToArray(); var unk = tokens.Where(x => x.TokenType == PhpTokenType.Unknown); #if DEBUG if (unk.Any()) { throw new InvalidOperationException(); } #endif var funcTokens = StartDelimiter == '(' ? PhpParser.GetGlobalFunctionCallIds(tokens) : PhpParser.GetArrayAccesses(tokens); for (int i = funcTokens.Length - 1; i >= 0; i--) { if (funcTokens[i].Lexeme != FunctionName) { continue; } if (StartDelimiter == '(') { PHP = PHP.Remove(funcTokens[i].Index, funcTokens[i].Lexeme.Length); PHP = PHP.Insert(funcTokens[i].Index, ReplacementName); } else { var tokenIndex = Array.IndexOf(tokens, tokens.Single(x => x.Index == funcTokens[i].Index)); PhpToken leftBracket = new PhpToken(), rightBracket = new PhpToken(); for (int j = tokenIndex + 1; j < tokens.Length; j++) { if (tokens[j].TokenType == PhpTokenType.LeftBracket) { leftBracket = tokens[j]; } else if (tokens[j].TokenType == PhpTokenType.RightBracket) { rightBracket = tokens[j]; break; } } if (leftBracket.TokenType != PhpTokenType.LeftBracket) { continue; } PHP = PHP.Insert(rightBracket.Index, ")"); PHP = PHP.Insert(leftBracket.Index + 1, ReplacementName + "("); } } }
public static Dictionary <string, List <string> > FindSuperglobalFields(PhpToken[] tokens) { var fields = PhpParser.GetSuperglobalFields(tokens); foreach (var superglobal in tokens .Where(x => x.TokenType == PhpTokenType.String || x.TokenType == PhpTokenType.HereDocString || x.TokenType == PhpTokenType.BacktickString) .Select(x => GetSuperGlobalFieldsFromText(x.Lexeme)) .SelectMany(x => x)) { fields[superglobal.Key].AddRange(superglobal.Value); } return(fields); }
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(); }); }