From 339d75a5bbe2123c0ed18a80d0affc9c0622bb52 Mon Sep 17 00:00:00 2001
From: matthiasgrimm <matthiasgrimm@users.sourceforge.net>
Date: Sat, 4 Jun 2005 21:46:21 +0200
Subject: [PATCH] extended search fix

This patch fixes a bug in the search code and did some
small optimizations.

The possibility to start a search with _only_ excluded
words (only words start with a minus sign) was disabled
because this seem to crash PHP 4.3.10 (at least the ppc
version :-()

darcs-hash:20050604194621-7ef76-10a5bb6a5b2804bcadcbffcdf2ea500db7ceeb91.gz
---
 inc/search.php | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/inc/search.php b/inc/search.php
index 654f1ab69..6a0b8fbd7 100644
--- a/inc/search.php
+++ b/inc/search.php
@@ -318,17 +318,21 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
     }
   }
   
-  $req  = count($poswords) ? $reg .= '^(?=.*?'.join(')(?=.*?',$poswords).')' : '^';
+  // if only negwords are given the preg_match_all() call will
+  // crash libapache-mod-php4 v4.3.10-13 (segfault). Until we
+  // got to the bottom of this problem, at least one posword is
+  // required.
+  if(!count($poswords)) return true;
+  
+  $reg  = count($poswords) ? '^(?=.*?'.join(')(?=.*?',$poswords).')' : '^';
   $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
-  $mark = '('.join('|',$poswords).')';
   
   //do the fulltext search
   $matches = array();
   if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
     //this is not the best way for snippet generation but the fastest I could find
     //split query and only use the first token
-    $q = preg_split('/\s+/',$opts['query'],2);
-    $q = $q[0];
+    $q = $poswords[0];
     $p = utf8_strpos($lctext,$q);
     $f = $p - 100;
     $l = utf8_strlen($q) + 200;
@@ -336,6 +340,7 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
     $snippet = '<span class="search_sep"> ... </span>'.
                htmlspecialchars(utf8_substr($text,$f,$l)).
                '<span class="search_sep"> ... </span>';
+    $mark    = '('.join('|',$poswords).')';
     $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet);
 
     $data[] = array(
-- 
GitLab