Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchController
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
0.00% covered (danger)
0.00%
0 / 1
 deleteTerm
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2
3/**
4 * The Admin Search Controller
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2023-10-26
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Controller\Administration\Api;
21
22use phpMyFAQ\Controller\AbstractController;
23use phpMyFAQ\Enums\PermissionType;
24use phpMyFAQ\Filter;
25use phpMyFAQ\Search;
26use phpMyFAQ\Session\Token;
27use phpMyFAQ\Translation;
28use Symfony\Component\HttpFoundation\JsonResponse;
29use Symfony\Component\HttpFoundation\Request;
30use Symfony\Component\HttpFoundation\Response;
31use Symfony\Component\Routing\Attribute\Route;
32
33final class SearchController extends AbstractController
34{
35    /**
36     * @throws \Exception
37     */
38    #[Route(path: 'search/term', name: 'admin.api.search.term', methods: ['DELETE'])]
39    public function deleteTerm(Request $request): JsonResponse
40    {
41        $this->userHasPermission(PermissionType::STATISTICS_VIEWLOGS);
42
43        $deleteData = $this->getJsonObject($request);
44
45        $search = new Search($this->configuration);
46
47        if (!Token::getInstance($this->session)->verifyToken('delete-searchterm', (string) ($deleteData->csrf ?? ''))) {
48            return $this->json(['error' => Translation::get(key: 'msgNoPermission')], Response::HTTP_UNAUTHORIZED);
49        }
50
51        $searchId = Filter::filterVar($deleteData->searchTermId ?? null, FILTER_VALIDATE_INT);
52
53        if (!is_int($searchId)) {
54            return $this->json(['error' => false], Response::HTTP_BAD_REQUEST);
55        }
56
57        if ($search->deleteSearchTermById($searchId)) {
58            return $this->json(['deleted' => $searchId], Response::HTTP_OK);
59        }
60
61        return $this->json(['error' => $searchId], Response::HTTP_BAD_REQUEST);
62    }
63}