mirror of
https://github.com/flarum/framework.git
synced 2024-12-11 13:05:50 +08:00
Escape string used in LIKE query
This commit is contained in:
parent
09528a38d0
commit
bc092c48d4
|
@ -90,6 +90,8 @@ class UserRepository
|
||||||
*/
|
*/
|
||||||
public function getIdsForUsername($string, User $actor = null)
|
public function getIdsForUsername($string, User $actor = null)
|
||||||
{
|
{
|
||||||
|
$string = $this->escapeLikeString($string);
|
||||||
|
|
||||||
$query = User::where('username', 'like', '%'.$string.'%')
|
$query = User::where('username', 'like', '%'.$string.'%')
|
||||||
->orderByRaw('username = ? desc', [$string])
|
->orderByRaw('username = ? desc', [$string])
|
||||||
->orderByRaw('username like ? desc', [$string.'%']);
|
->orderByRaw('username like ? desc', [$string.'%']);
|
||||||
|
@ -112,4 +114,15 @@ class UserRepository
|
||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escape special characters that can be used as wildcards in a LIKE query.
|
||||||
|
*
|
||||||
|
* @param string $string
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function escapeLikeString($string)
|
||||||
|
{
|
||||||
|
return str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user