From 462b6dbc4a1b57b11dac01b43757a3a8e69c8611 Mon Sep 17 00:00:00 2001
From: Cerdic <cedric@yterium.com>
Date: Tue, 18 Mar 2008 20:43:20 +0000
Subject: [PATCH] #1233 : ne pas generer une erreur sql sur recherche aberrante
 modifier le like pour que "logiciel  tendresse" trouve la meme chose que
 "logiciel tendresse" interpreter cela comme "contenant logiciel et tendresse
 dans cet ordre la" mais pas necessairement cote a cote assurer que la regexp
 trouve la meme chose

---
 ecrire/inc/prepare_recherche.php |  4 ++--
 ecrire/inc/rechercher.php        | 11 +++++++----
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/ecrire/inc/prepare_recherche.php b/ecrire/inc/prepare_recherche.php
index 78672cb0c7..d6534c2731 100644
--- a/ecrire/inc/prepare_recherche.php
+++ b/ecrire/inc/prepare_recherche.php
@@ -31,8 +31,8 @@ function inc_prepare_recherche_dist($recherche, $table='articles', $cond=false,
 	// traiter le cas {recherche?}
 	if ($cond AND !strlen($recherche))
 		return array("0 as points" /* as points */, /* where */ '');
-
-
+		
+	
 	$rechercher = false;
 
 	if (!isset($cache[$recherche][$table])){
diff --git a/ecrire/inc/rechercher.php b/ecrire/inc/rechercher.php
index c503e79fa0..4e436da4a6 100644
--- a/ecrire/inc/rechercher.php
+++ b/ecrire/inc/rechercher.php
@@ -132,21 +132,24 @@ function recherche_en_base($recherche='', $tables=NULL, $options=array(), $serve
 	if (!strlen($recherche))
 		return array();
 
+	$preg = '/'.$recherche.'/' . $options['preg_flags'];
+
 	// Si la chaine est inactive, on va utiliser LIKE pour aller plus vite
-	if (preg_quote($recherche, '/') == $recherche) {
+	// ou si l'expression reguliere est invalide
+	if (preg_quote($recherche, '/') == $recherche
+	OR (@preg_match($preg,'')===FALSE) ) {
 		$methode = 'LIKE';
 		$q = sql_quote(
 			"%"
-			. str_replace(array('%','_'), array('\%', '\_'), $recherche)
+			. preg_replace(",\s+,","%",str_replace(array('%','_'), array('\%', '\_'), trim($recherche)))
 			. "%"
 		);
+		$preg = '/'.preg_replace(",\s+,",".+",trim($recherche)).'/' . $options['preg_flags'];
 	} else {
 		$methode = 'REGEXP';
 		$q = sql_quote($recherche);
 	}
 
-	$preg = '/'.$recherche.'/' . $options['preg_flags'];
-
 	$jointures = $options['jointures']
 		? liste_des_jointures()
 		: array();
-- 
GitLab