Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Catch some UTF-8 exceptions
James Cole3653bdb0bc9a
diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.phpindex e1e1d58b7a..3d6b847a34 100644--- a/app/Http/Controllers/SearchController.php+++ b/app/Http/Controllers/SearchController.php@@ -47,7 +47,7 @@ final class SearchController extends Controllerapp('view')->share('showCategory', true);$this->middleware(static function ($request, $next) {app('view')->share('mainTitleIcon', 'bi-search');- app('view')->share('title', (string) trans('firefly.search'));+ app('view')->share('title', (string)trans('firefly.search'));return $next($request);});@@ -58,23 +58,24 @@ final class SearchController extends Controller** @return Factory|View*/- public function index(Request $request, SearchInterface $searcher): Factory|\Illuminate\Contracts\View\View+ public function index(Request $request, SearchInterface $searcher): Factory | \Illuminate\Contracts\View\View{// search params:- $fullQuery = (string) $request->input('search');- $fullQuery = substr($fullQuery, 0, 500);+ $fullQuery = (string)$request->input('search');+ $fullQuery = substr($fullQuery, 0, 500);if (is_array($request->input('search'))) {$fullQuery = '';}- $fullQuery = (string) $fullQuery;- $page = 0 === (int) $request->input('page') ? 1 : (int) $request->input('page');- $page = clamp(value: $page, min: 1, max: 2 ** 16);- $ruleId = (int) $request->input('rule');- $ruleChanged = false;+ $fullQuery = (string)$fullQuery;+ $fullQuery = mb_convert_encoding($fullQuery, 'UTF-8', 'UTF-8');;+ $page = 0 === (int)$request->input('page') ? 1 : (int)$request->input('page');+ $page = clamp(value: $page, min: 1, max: 2 ** 16);+ $ruleId = (int)$request->input('rule');+ $ruleChanged = false;// find rule, check if query is different, offer to update.- $ruleRepository = app(RuleRepositoryInterface::class);- $rule = $ruleRepository->find($ruleId);+ $ruleRepository = app(RuleRepositoryInterface::class);+ $rule = $ruleRepository->find($ruleId);if (null !== $rule) {$originalQuery = $ruleRepository->getSearchQuery($rule);if ($originalQuery !== $fullQuery) {@@ -89,9 +90,9 @@ final class SearchController extends Controller$excludedWords = $searcher->getExcludedWords();$operators = $searcher->getOperators();$invalidOperators = $searcher->getInvalidOperators();- $subTitle = (string) trans('breadcrumbs.search_result', ['query' => $fullQuery]);+ $subTitle = (string)trans('breadcrumbs.search_result', ['query' => $fullQuery]);- return view('search.index', [+ $res = view('search.index', ['words' => $words,'excludedWords' => $excludedWords,'operators' => $operators,@@ -103,6 +104,7 @@ final class SearchController extends Controller'ruleChanged' => $ruleChanged,'invalidOperators' => $invalidOperators,]);+ return $res;}/**@@ -112,13 +114,13 @@ final class SearchController extends Controller*/public function search(Request $request, SearchInterface $searcher): JsonResponse{- $entry = $request->input('query');+ $entry = $request->input('query');if (!is_scalar($entry)) {$entry = '';}- $fullQuery = (string) $entry;- $page = 0 === (int) $request->input('page') ? 1 : (int) $request->input('page');- $page = clamp(value: $page, min: 1, max: 2 ** 16);+ $fullQuery = (string)$entry;+ $page = 0 === (int)$request->input('page') ? 1 : (int)$request->input('page');+ $page = clamp(value: $page, min: 1, max: 2 ** 16);$searcher->parseQuery($fullQuery);@@ -127,7 +129,7 @@ final class SearchController extends Controller$hasPages = $groups->hasPages();$searchTime = round($searcher->searchTime(), 3); // in seconds$parameters = ['search' => $fullQuery];- $url = route('search.index').'?'.http_build_query($parameters);+ $url = route('search.index') . '?' . http_build_query($parameters);$groups->setPath($url);try {diff --git a/app/Support/Search/QueryParser/QueryParser.php b/app/Support/Search/QueryParser/QueryParser.phpindex e5e52d6735..fdc8fb8fee 100644--- a/app/Support/Search/QueryParser/QueryParser.php+++ b/app/Support/Search/QueryParser/QueryParser.php@@ -24,9 +24,10 @@ declare(strict_types=1);namespace FireflyIII\Support\Search\QueryParser;+use Illuminate\Support\Facades\Log;use LogicException;+use Safe\Exceptions\PcreException;use SensitiveParameter;-use function Safe\preg_split;/**@@ -36,22 +37,28 @@ use function Safe\preg_split;*/class QueryParser implements QueryParserInterface{- private int $position = 0;- private int $depth = 0;+ private int $position = 0;+ private int $depth = 0;private const int MAX_DEPTH = 32;// private string $query;- private array $chrArray = [];- private int $count = 0;+ private array $chrArray = [];+ private int $count = 0;public function parse(string $query): NodeGroup{// Log::debug(sprintf('Parsing query in QueryParser: "%s"', $query));// $this->query = $query;$this->position = 0;- $this->chrArray = preg_split('//u', $query, -1, PREG_SPLIT_NO_EMPTY);- $this->count = count($this->chrArray);+ try {+ $query = mb_convert_encoding($query, 'UTF-8', 'UTF-8');;+ $this->chrArray = preg_split('//u', $query, -1, PREG_SPLIT_NO_EMPTY);+ } catch (PcreException $e) {+ Log::error($e->getMessage());+ $this->chrArray = [];+ }+ $this->count = count($this->chrArray);return $this->buildNodeGroup(false);}@@ -73,7 +80,7 @@ class QueryParser implements QueryParserInterfaceif ('\\' === $char && '"' === $nextChar && '\\' !== $prevChar) {// Log::debug('Found a backslash and the next one is a double quote.');// escaped quote, pretend it's a normal char and continue two places (skipping the actual character).- $tokenUnderConstruction .= '\\'.$nextChar;+ $tokenUnderConstruction .= '\\' . $nextChar;$this->position += 2;continue;@@ -156,7 +163,7 @@ class QueryParser implements QueryParserInterfaceif ('' === $tokenUnderConstruction) {// In any other location, it's just a normal character$tokenUnderConstruction .= $char;- $skipNext = true;+ $skipNext = true;}if ('' !== $tokenUnderConstruction && !$skipNext) {// Log::debug(sprintf('Turns out that "%s" is a field name. Reset the token.', $tokenUnderConstruction));@@ -184,7 +191,7 @@ class QueryParser implements QueryParserInterface++$this->position;}- $finalNode = '' !== $tokenUnderConstruction || '' !== $fieldName ? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited) : null;+ $finalNode = '' !== $tokenUnderConstruction || '' !== $fieldName ? $this->createNode($tokenUnderConstruction, $fieldName, $prohibited) : null;return new NodeResult($finalNode, true);}@@ -195,7 +202,7 @@ class QueryParser implements QueryParserInterface$nodeResult = $this->buildNextNode($isSubquery);while ($nodeResult->node instanceof Node) {- $nodes[] = $nodeResult->node;+ $nodes[] = $nodeResult->node;if ($nodeResult->isSubqueryEnd) {break;}