using System; using System.Collections.Generic; using System.Text; using System.IO; // Search ASP // Written by Patrick Coston // February 2011 // patcoston@gmail.com // http://www.patcoston.com/ // This code is protected by the GNU General Public License // http://patcoston.com/daymoon/GNU.txt namespace SearchASP { class Program { static string RootDir = ""; // root directory of web site static string IncludeType = "file"; // type of include: "virtual" or "file" static int FilesParsed = 0; // number of files parsed so far static string[] Parsed = new string[1000]; // array of parsed files static bool ResultsFound = false; // no results were found static bool IgnoreCase = false; // case sensitive search // Parses the lines in a file // Name - the name of the file to parse // Find - the string you are looking for // Used to output includes, Sub names and Function names public static void ParseLines(string Name, string Find) { int p1; int LineNumber = 0; String Line2, Find2; StreamReader re = File.OpenText(Name); string Line = null; bool NameDisplayed = false; while ((Line = re.ReadLine()) != null) { LineNumber++; Line = Line.Trim(); if (Line.Length > 0) { if (IgnoreCase) { Line2 = Line.ToLower(); // lower case the line of text Find2 = Find.ToLower(); // lower case the search string p1 = Line2.IndexOf(Find2); // search line of text for search string } else p1 = Line.IndexOf(Find); if (p1 != (-1)) // if item found { if (!NameDisplayed) { Console.WriteLine("\n" + Name); // write file searching NameDisplayed = true; } Console.WriteLine(LineNumber.ToString() + ": " + Line); // write line in which it is found ResultsFound = true; // results were found } } } re.Close(); } // Search for // #include file="foo.asp" // #include virtual="foo.asp" public static string GetIncludePath(string Line) { int p1 = Line.IndexOf("#include") + 9; int p2 = Line.IndexOf("virtual", p1) + 8; if (p2 == 7) // virtual not found { IncludeType = "file"; p2 = Line.IndexOf("file", p1) + 5; } else IncludeType = "virtual"; int p3 = Line.IndexOf("\"", p2) + 1; int p4 = Line.IndexOf("\"", p3); int Len = p4 - p3; string Name = Line.Substring(p3, Len); return (Name); } // Returns the full path to an include file name // Converts the path used in the include statement to a full system path // Examples // ../../../include/settings.asp might be converted to f:\web\staging\root\include\settings.asp // login/accounts/settings/default.asp might be converted to c:\inetpub\wwwroot\krc\live3\login\accounts\settings\default.asp public static string GetFullPath(string Name) { string FullPath = RootDir; Name = Name.Replace("/", "\\"); if (IncludeType == "virtual") { string str = Name.Substring(0, 1); if (str != "\\") Name = "\\" + Name; FullPath += Name; } else { string CurrentDir = Directory.GetCurrentDirectory(); if (Name.Substring(0, 2) == "..") { while (Name.Substring(0, 2) == "..") { Name = Name.Substring(3, Name.Length - 3); // remove "..\\" Directory.SetCurrentDirectory(".."); // move up a directory CurrentDir = Directory.GetCurrentDirectory(); } } FullPath = CurrentDir + "\\" + Name; } return (FullPath); } // Sets the current directory and returns the current directory // Name - full path to include file public static string SetCurrentDir(string Name) { int p = 0; int LastIndex = 0; // look for file name by skipping past back slashes while (p != -1) { LastIndex = p; p = Name.IndexOf("\\", p + 2); } string Path = Name.Substring(0, LastIndex); // strip off file name leaving path to file Directory.SetCurrentDirectory(Path); return (Path); } // recursively parse include files within an asp page // Name - name of asp file to parse public static void ParseIncludes(string Name, string SearchString) { int Pos = 0; string Find = "#include"; string IncludePath = ""; string FullPath = ""; string CurrentDir = SetCurrentDir(Name); StreamReader re = File.OpenText(Name); string Line = null; while ((Line = re.ReadLine()) != null) { Line = Line.Trim(); if (Line.Length > 0) { Pos = Line.IndexOf(Find); if (Pos != -1) { IncludePath = GetIncludePath(Line); FullPath = GetFullPath(IncludePath); ParseFile(FullPath, SearchString); Directory.SetCurrentDirectory(CurrentDir); } } } re.Close(); } // File been parsed before? returns true/false public static bool ParsedBefore(string Name) { bool Before = false; for (int i = 0; i < FilesParsed; i++) if (Parsed[i] == Name) Before = true; if (!Before) { Parsed[FilesParsed] = Name; FilesParsed++; } return (Before); } // Name - filename to parse // Type - file for main file or file include, virtual for virtual include public static void ParseFile(string Name, string SearchString) { if (ParsedBefore(Name)) // don't parse the same include file more than once return; if (!File.Exists(Name)) { Console.WriteLine("\nFile " + Name + " does not exist\n"); return; } ParseLines(Name, SearchString); ParseIncludes(Name, SearchString); } // args[0] - full path to root of site // args[1] - name of asp file to parse // args[2] - string you are searching for public static int Main(string[] args) { if ((args.Length == 3) || (args.Length == 4)) { if ((args.Length == 4) && (args[3] == "-ignorecase")) IgnoreCase = true; RootDir = args[0]; string FullPath = Directory.GetCurrentDirectory() + "\\" + args[1]; ParseFile(FullPath, args[2]); if (!ResultsFound) Console.WriteLine("\nNo results were found for \"" + args[2] + "\""); } else { Console.WriteLine("Search ASP: Version 1.0"); Console.WriteLine("Syntax: searchasp [root] [file] [string] [-ignorecase]"); Console.WriteLine("root - full path to root of drive (where virtual include paths resolve)"); Console.WriteLine("file - name of asp file to search"); Console.WriteLine("string - string you are searching"); Console.WriteLine("-ignorecase - case insensitive search (optional)"); Console.WriteLine("\n"); Console.WriteLine("Examples:"); Console.WriteLine("searchasp c:\\web\\httpdocs default.asp leftColumn"); Console.WriteLine("searchasp x:\\staging\\project1 home.asp \"lorem ipsum\" -ignorecase"); Console.WriteLine("searchasp d:\\inetpub\\wwwroot welcome.asp \"id=\\\"mainContent\\\"\""); Console.WriteLine("\n"); Console.WriteLine("Instructions:"); Console.WriteLine("* This is a command line app"); Console.WriteLine("* Do not end path to root of site with a slash"); Console.WriteLine("* Add executable to your path"); Console.WriteLine("* Run from directory of page to parse"); Console.WriteLine("* Do not add a path to the file to parse"); } return 0; } } }