Esempio n. 1
0
        //implementation as of 2013-01-10, new version is above
        //public static IndexItemCollection Search(
        //    int siteId,
        //    bool isAdmin,
        //    List<string> userRoles,
        //    Guid featureGuid,
        //    string queryText,
        //    bool highlightResults,
        //    int highlightedFragmentSize,
        //    int pageNumber,
        //    int pageSize,
        //    int maxClauseCount,
        //    bool sortByPubDateDescending,
        //    out int totalHits,
        //    out bool invalidQuery)
        //{
        //    invalidQuery = false;
        //    totalHits = 0;
        //    string indexPath = GetIndexPath(siteId);
        //    IndexItemCollection results = new IndexItemCollection();
        //    if (string.IsNullOrEmpty(queryText))
        //    {
        //        return results;
        //    }
        //    bool useBackwardCompatibilityMode = WebConfigSettings.SearchUseBackwardCompatibilityMode;
        //    bool DisableSearchFeatureFilters = WebConfigSettings.DisableSearchFeatureFilters;
        //    bool IncludeModuleRoleFilters = WebConfigSettings.SearchIncludeModuleRoleFilters;
        //    Lucene.Net.Store.Directory d = new Lucene.Net.Store.SimpleFSDirectory(new DirectoryInfo(indexPath));
        //    //if (IndexReader.IndexExists(indexPath))
        //    if (IndexReader.IndexExists(d))
        //    {
        //        if (debugLog)
        //        {
        //            log.Debug("Entered Search, indexPath = " + indexPath);
        //        }
        //        long startTicks = DateTime.Now.Ticks;
        //        try
        //        {
        //            if (maxClauseCount != 1024)
        //            {
        //                //BooleanQuery.SetMaxClauseCount(maxClauseCount);
        //                BooleanQuery.MaxClauseCount = maxClauseCount;
        //            }
        //            BooleanQuery mainQuery = new BooleanQuery();
        //            if ((!isAdmin) && (!useBackwardCompatibilityMode))
        //            {
        //                AddRoleQueries(userRoles, mainQuery);
        //            }
        //            if ((!isAdmin) && (IncludeModuleRoleFilters))
        //            {
        //                AddModuleRoleQueries(userRoles, mainQuery);
        //            }
        //            //Query multiQuery = MultiFieldQueryParser.Parse(
        //            //    new string[] { queryText, queryText, queryText, queryText, queryText, queryText.Replace("*", string.Empty) },
        //            //    new string[] { "Title", "ModuleTitle", "contents", "PageName", "PageMetaDesc", "Keyword" },
        //            //    new StandardAnalyzer());
        //            Query multiQuery = MultiFieldQueryParser.Parse(
        //                Lucene.Net.Util.Version.LUCENE_30,
        //                new string[] { queryText, queryText, queryText, queryText, queryText, queryText.Replace("*", string.Empty) },
        //                new string[] { "Title", "ModuleTitle", "contents", "PageName", "PageMetaDesc", "Keyword" },
        //                new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
        //            //mainQuery.Add(multiQuery, BooleanClause.Occur.MUST);
        //            mainQuery.Add(multiQuery, Occur.MUST);
        //            if (!useBackwardCompatibilityMode)
        //            {
        //                Term beginDateStart = new Term("PublishBeginDate", DateTime.MinValue.ToString("s"));
        //                Term beginDateEnd = new Term("PublishBeginDate", DateTime.UtcNow.ToString("s"));
        //                //RangeQuery beginDateQuery = new RangeQuery(beginDateStart, beginDateEnd, true);
        //                TermRangeQuery beginDateQuery = new TermRangeQuery(
        //                    "PublishBeginDate",
        //                    DateTime.MinValue.ToString("s"),
        //                    DateTime.UtcNow.ToString("s"),
        //                    true,
        //                    true);
        //                //mainQuery.Add(beginDateQuery, BooleanClause.Occur.MUST);
        //                mainQuery.Add(beginDateQuery, Occur.MUST);
        //                Term endDateStart = new Term("PublishEndDate", DateTime.UtcNow.ToString("s"));
        //                Term endDateEnd = new Term("PublishEndDate", DateTime.MaxValue.ToString("s"));
        //                //RangeQuery endDateQuery = new RangeQuery(endDateStart, endDateEnd, true);
        //                TermRangeQuery endDateQuery = new TermRangeQuery(
        //                    "PublishEndDate",
        //                    DateTime.UtcNow.ToString("s"),
        //                    DateTime.MaxValue.ToString("s"),
        //                    true,
        //                    true);
        //                //mainQuery.Add(endDateQuery, BooleanClause.Occur.MUST);
        //                mainQuery.Add(endDateQuery, Occur.MUST);
        //            }
        //            if ((!DisableSearchFeatureFilters) && (featureGuid != Guid.Empty))
        //            {
        //                BooleanQuery featureFilter = new BooleanQuery();
        //                //featureFilter.Add(new TermQuery(new Term("FeatureId", featureGuid.ToString())), BooleanClause.Occur.MUST);
        //                featureFilter.Add(new TermQuery(new Term("FeatureId", featureGuid.ToString())), Occur.MUST);
        //                //mainQuery.Add(featureFilter, BooleanClause.Occur.MUST);
        //                mainQuery.Add(featureFilter, Occur.MUST);
        //            }
        //            Lucene.Net.Store.SimpleFSDirectory dir = new Lucene.Net.Store.SimpleFSDirectory(new DirectoryInfo(indexPath));
        //            //IndexSearcher searcher = new IndexSearcher(indexPath);
        //            IndexSearcher searcher = new IndexSearcher(dir);
        //            // a 0 based colection
        //            //Hits hits = searcher.Search(mainQuery);
        //            int maxResults = 500;
        //            //http://stackoverflow.com/questions/9872933/migrating-lucene-hitcollector-2-x-to-collector-3-x
        //            //TopScoreDocCollector collector = TopScoreDocCollector.Create(maxResults, true);
        //            TopDocs hits = searcher.Search(mainQuery, maxResults);
        //            int startHit = 0;
        //            if (pageNumber > 1)
        //            {
        //                startHit = ((pageNumber - 1) * pageSize);
        //            }
        //            //totalHits = hits.Length();
        //            totalHits = hits.TotalHits;
        //            int end = startHit + pageSize;
        //            if (totalHits <= end)
        //            {
        //                end = totalHits;
        //            }
        //            int itemsAdded = 0;
        //            int itemsToAdd = end;
        //            // in backward compatibility mode if multiple pages of results are found we may not be showing every user the correct
        //            // number of hits they can see as we only filter out the current page
        //            //we may decrement total hits if filtering results so keep the original count
        //            int actualHits = totalHits;
        //            if (!useBackwardCompatibilityMode)
        //            {
        //                // this new way is much cleaner
        //                //all filtering is done by query so the hitcount is true
        //                //whereas with the old way it could be wrong since there
        //                // were possibly results filtered out after the query returned.
        //                QueryScorer scorer = new QueryScorer(multiQuery);
        //                SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class='searchterm'>", "</span>");
        //                Highlighter highlighter = new Highlighter(formatter, scorer);
        //                //highlighter.SetTextFragmenter(new SimpleFragmenter(highlightedFragmentSize));
        //                highlighter.TextFragmenter = new SimpleFragmenter(highlightedFragmentSize);
        //                for (int i = startHit; i < itemsToAdd; i++)
        //                {
        //                    //IndexItem indexItem = new IndexItem(hits.Doc(i), hits.Score(i));
        //                    Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);
        //                    IndexItem indexItem = new IndexItem(doc, hits.ScoreDocs[i].Score);
        //                    if (highlightResults)
        //                    {
        //                        try
        //                        {
        //                            //TokenStream stream = new StandardAnalyzer().TokenStream("contents", new StringReader(hits.Doc(i).Get("contents")));
        //                            TokenStream stream = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30).TokenStream("contents", new StringReader(doc.Get("contents")));
        //                            //string highlightedResult = highlighter.GetBestFragment(stream, hits.Doc(i).Get("contents"));
        //                            string highlightedResult = highlighter.GetBestFragment(stream, doc.Get("contents"));
        //                            if (highlightedResult != null) { indexItem.Intro = highlightedResult; }
        //                        }
        //                        catch (NullReferenceException) { }
        //                    }
        //                    results.Add(indexItem);
        //                    itemsAdded += 1;
        //                }
        //            }
        //            else
        //            {
        //                //backward compatible with old indexes
        //                int filteredItems = 0;
        //                for (int i = startHit; i < itemsToAdd; i++)
        //                {
        //                    bool needToDecrementTotalHits = false;
        //                    Document doc = searcher.Doc(hits.ScoreDocs[i].Doc);
        //                    if (
        //                        (isAdmin)
        //                        || (WebUser.IsContentAdmin)
        //                        //|| (WebUser.IsInRoles(hits.Doc(i).Get("ViewRoles")))
        //                        || (WebUser.IsInRoles(doc.Get("ViewRoles")))
        //                        )
        //                    {
        //                        //IndexItem indexItem = new IndexItem(hits.Doc(i), hits.Score(i));
        //                        IndexItem indexItem = new IndexItem(doc, hits.ScoreDocs[i].Score);
        //                        if (
        //                        (DateTime.UtcNow > indexItem.PublishBeginDate)
        //                        && (DateTime.UtcNow < indexItem.PublishEndDate)
        //                        )
        //                        {
        //                            results.Add(indexItem);
        //                        }
        //                        else
        //                        {
        //                            needToDecrementTotalHits = true;
        //                        }
        //                    }
        //                    else
        //                    {
        //                        needToDecrementTotalHits = true;
        //                    }
        //                    //filtered out a result so need to decrement
        //                    if (needToDecrementTotalHits)
        //                    {
        //                        filteredItems += 1;
        //                        totalHits -= 1;
        //                        //we also are not getting as many results as the page size so if there are more items
        //                        //we should increment itemsToAdd
        //                        if ((itemsAdded + filteredItems) < actualHits)
        //                        {
        //                            itemsToAdd += 1;
        //                        }
        //                    }
        //                }
        //            }
        //            //searcher.Close();
        //            searcher.Dispose();
        //            results.ItemCount = itemsAdded;
        //            results.PageIndex = pageNumber;
        //            results.ExecutionTime = DateTime.Now.Ticks - startTicks;
        //        }
        //        catch (ParseException ex)
        //        {
        //            invalidQuery = true;
        //            log.Error("handled error for search terms " + queryText, ex);
        //            // these parser exceptions are generally caused by
        //            // spambots posting too much junk into the search form
        //            // heres an option to automatically ban the ip address
        //            HandleSpam(queryText, ex);
        //            return results;
        //        }
        //        catch (BooleanQuery.TooManyClauses ex)
        //        {
        //            invalidQuery = true;
        //            log.Error("handled error for search terms " + queryText, ex);
        //            return results;
        //        }
        //    }
        //    return results;
        //}
        private static void HandleSpam(string queryText, Exception ex)
        {
            bool autoBanSpamBots = ConfigHelper.GetBoolProperty("AutoBanSpambotsOnSearchErrors", false);

            if ((autoBanSpamBots)&&(IsSpam(queryText)))
            {
                if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
                {
                    BannedIPAddress b = new BannedIPAddress();
                    b.BannedIP = HttpContext.Current.Request.UserHostAddress;
                    b.BannedReason = "spambot autodetected";
                    b.BannedUtc = DateTime.UtcNow;
                    b.Save();

                    //String pathToCacheDependencyFile
                    //        = HttpContext.Current.Server.MapPath(
                    //    "~/Data/bannedipcachedependency.config");

                    //CacheHelper.TouchCacheFile(pathToCacheDependencyFile);

                    //log.Error(queryText, ex);
                    log.Info("spambot detected, ip address has been banned: " + HttpContext.Current.Request.UserHostAddress);
                }
            }
            else
            {
                //log.Error(queryText, ex);
                log.Info("spambot possibly detected, ip address was: " + HttpContext.Current.Request.UserHostAddress);

            }
        }
        void grdBannedIPAddresses_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView grid = (GridView)sender;

            int rowID = (int)grid.DataKeys[e.RowIndex].Value;
            TextBox txtBannedIP = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedIP");
            TextBox txtBannedUTC = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedUTC");
            TextBox txtBannedReason = (TextBox)grid.Rows[e.RowIndex].Cells[1].FindControl("txtBannedReason");

            BannedIPAddress bannedIPAddress = new BannedIPAddress(rowID);
            bannedIPAddress.BannedIP = txtBannedIP.Text;
            DateTime bannedTime = DateTime.UtcNow;

            DateTime.TryParse(txtBannedUTC.Text, out bannedTime);

            if (timeZone != null)
            {
                bannedTime = bannedTime.ToUtc(timeZone);
            }

            bannedIPAddress.BannedUtc = bannedTime;
            bannedIPAddress.BannedReason = txtBannedReason.Text;
            bannedIPAddress.Save();
            //String pathToCacheDependencyFile
            //        = HttpContext.Current.Server.MapPath(
            //    "~/Data/bannedipcachedependency.config");
            //CacheHelper.TouchCacheFile(pathToCacheDependencyFile);

            WebUtils.SetupRedirect(this, Request.RawUrl);
        }