diff --git a/nginx.conf b/nginx.conf index ea548cd9ba..db48e7be40 100644 --- a/nginx.conf +++ b/nginx.conf @@ -149,6 +149,7 @@ server { rewrite admin/api/elasticsearch/drop /admin/api/index.php last; rewrite admin/api/elasticsearch/import /admin/api/index.php last; rewrite admin/api/elasticsearch/statistics /admin/api/index.php last; + rewrite admin/api/search/term /admin/api/index.php last; rewrite admin/api/health-check /admin/api/index.php last; rewrite admin/api/updates /admin/api/index.php last; rewrite admin/api/update-check /admin/api/index.php last; diff --git a/phpmyfaq/.htaccess b/phpmyfaq/.htaccess index 469ca40b4c..8255966622 100644 --- a/phpmyfaq/.htaccess +++ b/phpmyfaq/.htaccess @@ -153,12 +153,13 @@ RewriteRule admin/api/content/attachments/upload admin/api/index.php RewriteRule admin/api/content/comments admin/api/index.php RewriteRule admin/api/content/images admin/api/index.php RewriteRule admin/api/content/markdown admin/api/index.php +RewriteRule admin/api/dashboard/versions admin/api/index.php +RewriteRule admin/api/dashboard/visits admin/api/index.php RewriteRule admin/api/elasticsearch/create admin/api/index.php RewriteRule admin/api/elasticsearch/drop admin/api/index.php RewriteRule admin/api/elasticsearch/import admin/api/index.php RewriteRule admin/api/elasticsearch/statistics admin/api/index.php -RewriteRule admin/api/dashboard/versions admin/api/index.php -RewriteRule admin/api/dashboard/visits admin/api/index.php +RewriteRule admin/api/search/term admin/api/index.php RewriteRule admin/api/health-check admin/api/index.php RewriteRule admin/api/versions admin/api/index.php RewriteRule admin/api/update-check admin/api/index.php diff --git a/phpmyfaq/admin/api/search.php b/phpmyfaq/admin/api/search.php deleted file mode 100644 index 2700aa3349..0000000000 --- a/phpmyfaq/admin/api/search.php +++ /dev/null @@ -1,61 +0,0 @@ - - * @copyright 2011-2023 phpMyFAQ Team - * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 - * @link https://www.phpmyfaq.de - * @since 2011-08-24 - */ - -use phpMyFAQ\Filter; -use phpMyFAQ\Search; -use phpMyFAQ\Session\Token; -use phpMyFAQ\Translation; -use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Response; - -if (!defined('IS_VALID_PHPMYFAQ')) { - http_response_code(400); - exit(); -} - -// -// Create Request & Response -// -$response = new JsonResponse(); -$request = Request::createFromGlobals(); - -$ajaxAction = Filter::filterVar($request->query->get('ajaxaction'), FILTER_SANITIZE_SPECIAL_CHARS); - -$search = new Search($faqConfig); - -if ($ajaxAction === 'delete_searchterm') { - $deleteData = json_decode(file_get_contents('php://input', true)); - - if (!Token::getInstance()->verifyToken('delete-searchterms', $deleteData->csrf)) { - $response->setStatusCode(Response::HTTP_BAD_REQUEST); - $response->setData(['error' => Translation::get('err_NotAuth')]); - $response->send(); - exit(1); - } - - $searchId = Filter::filterVar($deleteData->searchTermId, FILTER_VALIDATE_INT); - - if ($search->deleteSearchTermById($searchId)) { - $response->setStatusCode(Response::HTTP_OK); - $response->setData(['deleted' => $searchId]); - } else { - $response->setStatusCode(Response::HTTP_BAD_REQUEST); - $response->setData(['error' => $searchId]); - } - $response->send(); -} diff --git a/phpmyfaq/admin/assets/src/statistics.js b/phpmyfaq/admin/assets/src/statistics.js index 04e775bf91..98f29aa60c 100644 --- a/phpmyfaq/admin/assets/src/statistics.js +++ b/phpmyfaq/admin/assets/src/statistics.js @@ -27,7 +27,7 @@ export const handleStatistics = () => { const csrf = event.target.getAttribute('data-csrf-token'); if (confirm('Are you sure?')) { - fetch('index.php?action=ajax&ajax=search&ajaxaction=delete_searchterm', { + fetch('./api/search/term', { method: 'DELETE', headers: { Accept: 'application/json, text/plain, */*', @@ -39,22 +39,19 @@ export const handleStatistics = () => { }), }) .then(async (response) => { - if (response.status === 200) { + if (response.ok) { return response.json(); } - throw new Error('Network response was not ok.'); + throw new Error('Network response was not ok: ', { cause: { response } }); }) .then((response) => { const row = document.getElementById(`row-search-id-${response.deleted}`); row.addEventListener('click', () => (row.style.opacity = '0')); row.addEventListener('transitionend', () => row.remove()); }) - .catch((error) => { - const table = document.querySelector('.table'); - table.insertAdjacentElement( - 'afterend', - addElement('div', { classList: 'alert alert-danger', innerText: error }) - ); + .catch(async (error) => { + const errorMessage = await error.cause.response.json(); + console.error(errorMessage.error); }); } }); diff --git a/phpmyfaq/admin/index.php b/phpmyfaq/admin/index.php index 750df9b5c1..1efab3d271 100755 --- a/phpmyfaq/admin/index.php +++ b/phpmyfaq/admin/index.php @@ -260,10 +260,6 @@ case 'records': require 'api/faqs.php'; break; - // Search - case 'search': - require 'api/search.php'; - break; // Users case 'user': require 'api/user.php'; diff --git a/phpmyfaq/admin/stat.search.php b/phpmyfaq/admin/stat.search.php index 2114a4e493..eeac5102e3 100644 --- a/phpmyfaq/admin/stat.search.php +++ b/phpmyfaq/admin/stat.search.php @@ -129,7 +129,7 @@ ++$displayedCounter; $num = round(($searchItem['number'] * 100 / $searchesCount), 2); - $csrfToken = Token::getInstance()->getTokenString('delete-searchterms'); + $csrfToken = Token::getInstance()->getTokenString('delete-searchterm'); ?> diff --git a/phpmyfaq/src/admin-routes.php b/phpmyfaq/src/admin-routes.php index 03bb77284e..11673b4ad0 100644 --- a/phpmyfaq/src/admin-routes.php +++ b/phpmyfaq/src/admin-routes.php @@ -21,6 +21,7 @@ use phpMyFAQ\Controller\Administration\ElasticsearchController; use phpMyFAQ\Controller\Administration\ImageController; use phpMyFAQ\Controller\Administration\MarkdownController; +use phpMyFAQ\Controller\Administration\SearchController; use phpMyFAQ\Controller\Administration\UpdateController; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -111,6 +112,14 @@ new Route('/elasticsearch/statistics', ['_controller' => [ElasticsearchController::class, 'statistics']]) ); +// +// Search API +// +$routes->add( + 'admin.api.search.term', + new Route('/search/term', ['_controller' => [SearchController::class, 'deleteTerm'], '_methods' => 'DELETE']) +); + // // Update API // diff --git a/phpmyfaq/src/phpMyFAQ/Controller/Administration/SearchController.php b/phpmyfaq/src/phpMyFAQ/Controller/Administration/SearchController.php new file mode 100644 index 0000000000..c7a2aa3ec9 --- /dev/null +++ b/phpmyfaq/src/phpMyFAQ/Controller/Administration/SearchController.php @@ -0,0 +1,44 @@ +getContent()); + + $search = new Search(Configuration::getConfigurationInstance()); + + if (!Token::getInstance()->verifyToken('delete-searchterm', $deleteData->csrf)) { + $response->setStatusCode(Response::HTTP_BAD_REQUEST); + $response->setData(['error' => Translation::get('err_NotAuth')]); + return $response; + } + + $searchId = Filter::filterVar($deleteData->searchTermId, FILTER_VALIDATE_INT); + + if ($search->deleteSearchTermById($searchId)) { + $response->setStatusCode(Response::HTTP_OK); + $response->setData(['deleted' => $searchId]); + } else { + $response->setStatusCode(Response::HTTP_BAD_REQUEST); + $response->setData(['error' => $searchId]); + } + + return $response; + } +}