Skip to content

Commit df54d00

Browse files
committed
fix: corrected query, closes #4310
1 parent a34efc1 commit df54d00

5 files changed

Lines changed: 15 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This is a log of major user-visible changes in each phpMyFAQ release.
88

99
### phpMyFAQ v4.1.4 - unreleased
1010

11+
- updated third party dependencies (Thorsten)
1112
- fixed bugs (Thorsten)
1213

1314
### phpMyFAQ v4.1.3 – 2026-05-14

phpmyfaq/src/phpMyFAQ/Search/Database/PdoPgsql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function search(string $searchTerm): mixed
7171
FROM
7272
%s %s %s %s
7373
WHERE
74-
(%s) ILIKE ('%%%s%%') ESCAPE '\\'
74+
(%s) ILIKE ('%%%s%%') ESCAPE '='
7575
%s
7676
%s",
7777
$columns,

phpmyfaq/src/phpMyFAQ/Search/Database/Pgsql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function search(string $searchTerm): mixed
7272
FROM
7373
%s %s %s %s
7474
WHERE
75-
(%s) ILIKE ('%%%s%%') ESCAPE '\\'
75+
(%s) ILIKE ('%%%s%%') ESCAPE '='
7676
%s
7777
%s",
7878
$columns,

phpmyfaq/src/phpMyFAQ/Search/SearchDatabase.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function getMatchClause(string $searchTerm = ''): string
274274
}
275275

276276
$where = sprintf(
277-
"%s%s LIKE '%%%s%%' ESCAPE '\\'",
277+
"%s%s LIKE '%%%s%%' ESCAPE '='",
278278
$where,
279279
$this->matchingColumns[$j],
280280
self::escapeLikeWildcards($this->configuration->getDb()->escape($keys[$i])),
@@ -299,9 +299,16 @@ public function disableRelevance(): void
299299
/**
300300
* Escapes LIKE wildcard metacharacters (%, _) in a search term
301301
* to prevent LIKE wildcard injection.
302+
*
303+
* Uses '=' as the LIKE escape character (see the ESCAPE clauses in the
304+
* driver queries). A backslash cannot be used here: in MySQL/MariaDB
305+
* string literals a trailing backslash escapes the closing quote, which
306+
* breaks the query, while SQLite rejects a doubled backslash as a
307+
* multi-character ESCAPE. '=' is literal in every supported database and
308+
* is not a LIKE metacharacter.
302309
*/
303310
protected static function escapeLikeWildcards(string $term): string
304311
{
305-
return str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $term);
312+
return str_replace(['=', '%', '_'], ['==', '=%', '=_'], $term);
306313
}
307314
}

tests/phpMyFAQ/Search/SearchDatabaseTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testGetMatchClause()
149149
{
150150
$this->searchDatabase->setMatchingColumns(['faqdata.author']);
151151
$this->assertEquals(
152-
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\')",
152+
" (faqdata.author LIKE '%Thorsten%' ESCAPE '=')",
153153
$this->searchDatabase->getMatchClause('Thorsten'),
154154
);
155155
$this->assertIsString($this->searchDatabase->getMatchClause('Thorsten'));
@@ -159,7 +159,7 @@ public function testGetMatchClauseWithTwoSearchTerms()
159159
{
160160
$this->searchDatabase->setMatchingColumns(['faqdata.author']);
161161
$this->assertEquals(
162-
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\') OR (faqdata.author LIKE '%Rinne%' ESCAPE '\\')",
162+
" (faqdata.author LIKE '%Thorsten%' ESCAPE '=') OR (faqdata.author LIKE '%Rinne%' ESCAPE '=')",
163163
$this->searchDatabase->getMatchClause('Thorsten Rinne'),
164164
);
165165
$this->assertIsString($this->searchDatabase->getMatchClause('Thorsten'));
@@ -169,7 +169,7 @@ public function testGetMatchClauseWithTwoColumns()
169169
{
170170
$this->searchDatabase->setMatchingColumns(['faqdata.author', 'faqdata.thema']);
171171
$this->assertEquals(
172-
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\' OR faqdata.thema LIKE '%Thorsten%' ESCAPE '\\')",
172+
" (faqdata.author LIKE '%Thorsten%' ESCAPE '=' OR faqdata.thema LIKE '%Thorsten%' ESCAPE '=')",
173173
$this->searchDatabase->getMatchClause('Thorsten'),
174174
);
175175
$this->assertIsString($this->searchDatabase->getMatchClause('Thorsten'));

0 commit comments

Comments
 (0)