Skip to content

Commit 4db5018

Browse files
committed
MAGETWO-44555: Exception in Advanced Search if used special symbols
- Fixed Binder and Cleaner
1 parent 3d95c89 commit 4db5018

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

lib/internal/Magento/Framework/Search/Adapter/Mysql/Query/Builder/Match.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
use Magento\Framework\Search\Adapter\Mysql\ScoreBuilder;
1313
use Magento\Framework\Search\Request\Query\BoolExpression;
1414
use Magento\Framework\Search\Request\QueryInterface as RequestQueryInterface;
15+
use Zend_Db_Expr;
1516

1617
class Match implements QueryInterface
1718
{
18-
const SPECIAL_CHARACTERS = '-+~/\\<>\'":*$#@()!,.?`=%';
19+
const SPECIAL_CHARACTERS = '-+~/\<>\'":*$#@()!,.?`=%^';
1920

2021
const MINIMAL_CHARACTER_LENGTH = 3;
2122

2223
/**
2324
* @var string[]
2425
*/
25-
private $replaceSymbols = [];
26+
private $pattern = [];
2627

2728
/**
2829
* @var ResolverInterface
@@ -50,7 +51,12 @@ public function __construct(
5051
$fulltextSearchMode = Fulltext::FULLTEXT_MODE_BOOLEAN
5152
) {
5253
$this->resolver = $resolver;
53-
$this->replaceSymbols = str_split(self::SPECIAL_CHARACTERS, 1);
54+
$characters = str_split(self::SPECIAL_CHARACTERS, 1);
55+
foreach ($characters as $key => $value) {
56+
$characters[$key] = "\\{$value}";
57+
}
58+
$characters = implode('', $characters);
59+
$this->pattern = $characters = "/([{$characters}])/";
5460
$this->fulltextHelper = $fulltextHelper;
5561
$this->fulltextSearchMode = $fulltextSearchMode;
5662
}
@@ -85,7 +91,7 @@ public function build(
8591

8692
$matchQuery = $this->fulltextHelper->getMatchQuery(
8793
$columns,
88-
$queryValue,
94+
new Zend_Db_Expr($queryValue),
8995
$this->fulltextSearchMode
9096
);
9197
$scoreBuilder->addCondition($matchQuery, true);
@@ -106,7 +112,7 @@ public function build(
106112
*/
107113
protected function prepareQuery($queryValue, $conditionType)
108114
{
109-
$queryValue = str_replace($this->replaceSymbols, ' ', $queryValue);
115+
$queryValue = preg_replace($this->pattern, '\\\\$1', $queryValue);
110116

111117
$stringPrefix = '';
112118
if ($conditionType === BoolExpression::QUERY_CONDITION_MUST) {

lib/internal/Magento/Framework/Search/Request/Binder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ private function processData($data, $bindData)
8181
} else {
8282
$data[$key] = $bindValue;
8383
}
84+
$data['is_bind'] = true;
8485
}
8586
}
8687
}

lib/internal/Magento/Framework/Search/Request/Cleaner.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ private function cleanQuery($queryName)
9090
}
9191
break;
9292
case QueryInterface::TYPE_MATCH:
93-
if (preg_match('/\$(.+)\$/si', $query['value'], $matches)
94-
&& !preg_match('/^\$+$/si', $query['value'], $matches)) {
93+
if (!array_key_exists('is_bind', $query)) {
9594
unset($this->requestData['queries'][$queryName]);
9695
}
9796
break;
@@ -131,8 +130,9 @@ private function cleanAggregations()
131130
foreach ($this->requestData['aggregations'] as $aggregationName => $aggregationValue) {
132131
switch ($aggregationValue['type']) {
133132
case 'dynamicBucket':
134-
if (is_string($aggregationValue['method'])
135-
&& preg_match('/\$(.+)\$/si', $aggregationValue['method'])
133+
if (
134+
!array_key_exists('is_bind', $aggregationValue)
135+
&& is_string($aggregationValue['method'])
136136
) {
137137
unset($this->requestData['aggregations'][$aggregationName]);
138138
}

0 commit comments

Comments
 (0)