using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Xml; using System.Xml.XPath; using System.Collections; using System.Text.RegularExpressions; using System.IO; using System.Text; /* Copyright (C) Hannon Hill Corporation, hannonhill.com, August 2006 This is a script from hannonhill.com. You will find this and many other scripts at our website as part of our content management system. Terms of use: You are free to use this script as long as the copyright message is kept intact. However, you may not redistribute, sell or repost it without our permission. There are no warranties and you use it at your own risk. */ public partial class _Default : System.Web.UI.Page { string logFilePath; long logFileSize; String frameFilePath; Int32 resultsPerPage; String resultTemplate; String noResultsTemplate; String headerTemplate; String nextPageTemplate; String previousPageTemplate; // The base directory that this script runs from static String basePath = System.AppDomain.CurrentDomain.BaseDirectory; // Supposed to get the URL location of the current script static String scriptPath = "search.aspx"; //basePath + System.AppDomain.CurrentDomain.ActivationContext.Identity; protected void Page_Load(object sender, EventArgs e) { Label1.Text = ""; String configFilename = "search-config.xml"; // Name of the configuration page XPathDocument configDoc = new XPathDocument(basePath + configFilename); XPathNavigator configNav = configDoc.CreateNavigator(); XPathNavigator config = configNav.SelectSingleNode("/config"); frameFilePath = config.SelectSingleNode("frame_file").Value; resultsPerPage = Convert.ToInt32(config.SelectSingleNode("results_per_page").Value); resultTemplate = config.SelectSingleNode("result_template").Value; noResultsTemplate = config.SelectSingleNode("noresults_template").Value; headerTemplate = config.SelectSingleNode("header_template").Value; nextPageTemplate = config.SelectSingleNode("next_page_template").Value; previousPageTemplate = config.SelectSingleNode("prev_page_template").Value; logFilePath = config.SelectSingleNode("synced-files/file/path").Value; logFileSize = config.SelectSingleNode("log_size").Value != "" ? Convert.ToInt64(config.SelectSingleNode("log_size").Value) : 3000; int start = Request.QueryString["n"] != null ? Convert.ToInt32(Request.QueryString["n"]) : 0; String search_string = Request.QueryString["s"]; int search_database_index = Convert.ToInt32(Request.QueryString["d"]); ArrayList dbList = new ArrayList(); // Find path to each database file from the configuration page XPathNodeIterator databaseIterator = configNav.Select("/config/files/file"); while (databaseIterator.MoveNext()) { dbList.Add(databaseIterator.Current.SelectSingleNode(".").Value); } // Convert dbList to an array of strings object[] objDatabases = dbList.ToArray(); string[] databases = new string[objDatabases.Length]; for (int i = 0; i < objDatabases.Length; i++) { databases[i] = (string)objDatabases[i]; } string searchDatabase = GetSearchDatabase(search_database_index, databases); ArrayList results = getMatches(search_string, searchDatabase); results.Sort(); // Sort the search results in order of relevance string output = readFileToString(frameFilePath); string oResults = ""; int totalResults = results.Count; int startingResult = start + 1; int endingResult = Math.Min(totalResults, start + resultsPerPage); // If there were no results, print out the "No Results Template" if (totalResults <= 0) { oResults += noResultsTemplate; } // Otherwise, include the header template before printing out the search results else { oResults += headerTemplate; } //SearchResult[] searchResults = results.ToArray(System.Type.GetType("SearchResult")); object[] objSearchResults = results.ToArray(); SearchResult[] searchResults = new SearchResult[objSearchResults.Length]; for (int i = 0; i < objSearchResults.Length; i++) { searchResults[i] = (SearchResult)objSearchResults[i]; } // Print each search result for (int i = start; i < Math.Min(results.Count, start + resultsPerPage); i++) { oResults += formatResultTemplate(resultTemplate, searchResults[i]); } int previousPageStart = start - resultsPerPage; int nextPageStart = start + resultsPerPage; int resultsToGo = Math.Min(results.Count - nextPageStart, resultsPerPage); if (start != 0) { oResults += "" + previousPageTemplate + ""; } if (start != 0 && resultsToGo > 0) { oResults += " "; } if (resultsToGo > 0) { oResults += "" + nextPageTemplate + ""; } output = output.Replace("{$SEARCH}", oResults); output = output.Replace("{$QUERY}", search_string); output = output.Replace("{$START_RESULT}", startingResult.ToString()); output = output.Replace("{$END_RESULT}", endingResult.ToString()); output = output.Replace("{$TOTAL_RESULTS}", totalResults.ToString()); output = output.Replace("{$PER_PAGE}", resultsPerPage.ToString()); output = output.Replace("{$RESULTS_ON_NEXT_PAGE}", resultsToGo.ToString()); // Replace the
content with the content of the output // (generated from the frame HTML file and code above) Header.InnerHtml = getTagContent(output, "head"); // Put the output's body content into the body of the script page Label1.Text = getTagContent(output, "body"); if (logFilePath != "") { logQuery(search_string); } } protected string formatResultTemplate(string template, SearchResult result) { string templateContent = template; while (substringAfter(templateContent, "%%%") != "") { string tagInsert = substringBefore(substringAfter(templateContent, "%%%"), "%%%"); string replaceStr = "%%%" + tagInsert + "%%%"; string newStr = getTagContent(result.PageContent, tagInsert); templateContent = templateContent.Replace(replaceStr, newStr); } return templateContent; } public void logQuery(String query) { FileStream file = new FileStream(basePath + logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamReader sReader = new StreamReader(file); string logContent = sReader.ReadToEnd(); logContent = query + "\n" + logContent; byte[] writeBytes = new UTF8Encoding(true).GetBytes(logContent); int writeLength = Math.Min(writeBytes.Length, Convert.ToInt32(logFileSize)); file.SetLength(writeLength); //file.Flush(); file.Seek(0, SeekOrigin.Begin); file.Write(writeBytes, 0, writeLength); file.Close(); } protected ArrayList getMatches(string searchString, string searchDatabase) { ArrayList results = new ArrayList(); if (searchString != "") { string[] words = searchString.Split(new string[] { " " }, StringSplitOptions.None); string[] pages = searchDatabase.Split(new string[] { "