From 99dbd4abe7f727015cb90b7034f27379ec41bd29 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 30 Sep 2015 20:30:46 -0500
Subject: [PATCH] search: Only quote email addresses in BOOLEAN mode

---
 include/class.search.php | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/include/class.search.php b/include/class.search.php
index dbf1f7eac..e4106eaee 100644
--- a/include/class.search.php
+++ b/include/class.search.php
@@ -306,7 +306,7 @@ class MysqlSearchBackend extends SearchBackend {
     // Quote things like email addresses
     function quote($query) {
         $parts = array();
-        if (!preg_match_all('`([^\s"\']+)|"[^"]*"|\'[^\']*\'`', $query, $parts,
+        if (!preg_match_all('`(?:([^\s"\']+)|"[^"]*"|\'[^\']*\')(\s*)`', $query, $parts,
                 PREG_SET_ORDER))
             return $query;
 
@@ -319,9 +319,9 @@ class MysqlSearchBackend extends SearchBackend {
                 $char = strpos($m[1], '"') ? "'" : '"';
                 $m[0] = $char . $m[0] . $char;
             }
-            $results[] = $m[0];
+            $results[] = $m[0].$m[2];
         }
-        return implode(' ', $results);
+        return implode('', $results);
     }
 
     function find($query, QuerySet $criteria, $addRelevance=true) {
@@ -330,10 +330,6 @@ class MysqlSearchBackend extends SearchBackend {
         $criteria = clone $criteria;
 
         $mode = ' IN NATURAL LANGUAGE MODE';
-        // If using boolean operators, search in boolean mode. This regex
-        // will ensure proper placement of operators, whitespace, and quotes
-        // in an effort to avoid crashing the query at MySQL
-        $query = $this->quote($query);
 
         // According to the MySQL full text boolean mode, this grammar is
         // assumed:
@@ -357,6 +353,10 @@ class MysqlSearchBackend extends SearchBackend {
         if (preg_match('`(^|\s)["()<>~+-]`u', $query, $T = array())
             && preg_match("`^{$BOOLEAN}$`u", $query, $T = array())
         ) {
+            // If using boolean operators, search in boolean mode. This regex
+            // will ensure proper placement of operators, whitespace, and quotes
+            // in an effort to avoid crashing the query at MySQL
+            $query = $this->quote($query);
             $mode = ' IN BOOLEAN MODE';
         }
         #elseif (count(explode(' ', $query)) == 1)
-- 
GitLab