Skip to content
Snippets Groups Projects
Commit 99dbd4ab authored by Jared Hancock's avatar Jared Hancock
Browse files

search: Only quote email addresses in BOOLEAN mode

parent 5e333943
No related branches found
No related tags found
No related merge requests found
...@@ -306,7 +306,7 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -306,7 +306,7 @@ class MysqlSearchBackend extends SearchBackend {
// Quote things like email addresses // Quote things like email addresses
function quote($query) { function quote($query) {
$parts = array(); $parts = array();
if (!preg_match_all('`([^\s"\']+)|"[^"]*"|\'[^\']*\'`', $query, $parts, if (!preg_match_all('`(?:([^\s"\']+)|"[^"]*"|\'[^\']*\')(\s*)`', $query, $parts,
PREG_SET_ORDER)) PREG_SET_ORDER))
return $query; return $query;
...@@ -319,9 +319,9 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -319,9 +319,9 @@ class MysqlSearchBackend extends SearchBackend {
$char = strpos($m[1], '"') ? "'" : '"'; $char = strpos($m[1], '"') ? "'" : '"';
$m[0] = $char . $m[0] . $char; $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) { function find($query, QuerySet $criteria, $addRelevance=true) {
...@@ -330,10 +330,6 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -330,10 +330,6 @@ class MysqlSearchBackend extends SearchBackend {
$criteria = clone $criteria; $criteria = clone $criteria;
$mode = ' IN NATURAL LANGUAGE MODE'; $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 // According to the MySQL full text boolean mode, this grammar is
// assumed: // assumed:
...@@ -357,6 +353,10 @@ class MysqlSearchBackend extends SearchBackend { ...@@ -357,6 +353,10 @@ class MysqlSearchBackend extends SearchBackend {
if (preg_match('`(^|\s)["()<>~+-]`u', $query, $T = array()) if (preg_match('`(^|\s)["()<>~+-]`u', $query, $T = array())
&& preg_match("`^{$BOOLEAN}$`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'; $mode = ' IN BOOLEAN MODE';
} }
#elseif (count(explode(' ', $query)) == 1) #elseif (count(explode(' ', $query)) == 1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment