Skip to content

Commit

Permalink
refactor: added controller for configuration, removed old code
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Oct 27, 2023
1 parent 93cddb2 commit 473d5ca
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
1 change: 1 addition & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ server {
# Administration API
rewrite admin/api/category/permissions /admin/api/index.php last;
rewrite admin/api/category/update-order /admin/api/index.php last;
rewrite admin/api/configuration/send-test-mail /admin/api/index.php last;
rewrite admin/api/content/attachments /admin/api/index.php last;
rewrite admin/api/content/attachments/upload /admin/api/index.php last;
rewrite admin/api/content/comments /admin/api/index.php last;
Expand Down
1 change: 1 addition & 0 deletions phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ RewriteRule api/setup/update-database api/index.php
# Administration API
RewriteRule admin/api/category/permissions admin/api/index.php
RewriteRule admin/api/category/update-order admin/api/index.php
RewriteRule admin/api/configuration/send-test-mail admin/api/index.php
RewriteRule admin/api/content/attachments admin/api/index.php
RewriteRule admin/api/content/attachments/upload admin/api/index.php
RewriteRule admin/api/content/comments admin/api/index.php
Expand Down
29 changes: 0 additions & 29 deletions phpmyfaq/admin/api/configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,33 +319,4 @@
$response->setData($payload);
$response->send();
break;

case 'send-test-mail':
$json = file_get_contents('php://input', true);
$postData = json_decode($json);

if (!Token::getInstance()->verifyToken('configuration', $postData->csrf)) {
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
$response->setData(['error' => Translation::get('err_NotAuth')]);
$response->send();
exit();
}

try {
$mailer = new Mail($faqConfig);
$mailer->setReplyTo($faqConfig->getAdminEmail());
$mailer->addTo($faqConfig->getAdminEmail());
$mailer->subject = $faqConfig->getTitle() . ': Mail test successful.';
$mailer->message = 'It works on my machine. 🚀';
$result = $mailer->send();

$response->setStatusCode(Response::HTTP_OK);
$response->setData(['success' => $result]);
} catch (Exception | TransportExceptionInterface $e) {
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
$response->setData(['error' => $e->getMessage()]);
}

$response->send();
break;
}
2 changes: 1 addition & 1 deletion phpmyfaq/admin/assets/js/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const handleSendTestMail = () => {
if (button) {
const csrf = document.querySelector('#pmf-csrf-token').value;

fetch('index.php?action=ajax&ajax=config&ajaxaction=send-test-mail', {
fetch('./api/configuration/send-test-mail', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
Expand Down
15 changes: 15 additions & 0 deletions phpmyfaq/src/admin-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use phpMyFAQ\Controller\Administration\AttachmentController;
use phpMyFAQ\Controller\Administration\CategoryController;
use phpMyFAQ\Controller\Administration\CommentController;
use phpMyFAQ\Controller\Administration\ConfigurationController;
use phpMyFAQ\Controller\Administration\DashboardController;
use phpMyFAQ\Controller\Administration\ElasticsearchController;
use phpMyFAQ\Controller\Administration\GroupController;
Expand Down Expand Up @@ -82,6 +83,20 @@
new Route('/content/comments', ['_controller' => [CommentController::class, 'delete'], '_methods' => 'DELETE'])
);

//
// Configuration API
//
$routes->add(
'admin.api.configuration.send-test-mail',
new Route(
'/configuration/send-test-mail',
[
'_controller' => [ConfigurationController::class, 'sendTestMail'],
'_methods' => 'POST'
]
)
);

//
// Image API
//
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace phpMyFAQ\Controller\Administration;

use phpMyFAQ\Configuration;
use phpMyFAQ\Controller;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Mail;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Routing\Annotation\Route;

class ConfigurationController extends Controller
{
#[Route('admin/api/configuration/send-test-mail')]
public function sendTestMail(Request $request): JsonResponse
{
$response = new JsonResponse();
$configuration = Configuration::getConfigurationInstance();

$data = json_decode($request->getContent());

if (!Token::getInstance()->verifyToken('configuration', $data->csrf)) {
$response->setStatusCode(Response::HTTP_UNAUTHORIZED);
$response->setData(['error' => Translation::get('err_NotAuth')]);
return $response;
}

try {
$mailer = new Mail($configuration);
$mailer->setReplyTo($configuration->getAdminEmail());
$mailer->addTo($configuration->getAdminEmail());
$mailer->subject = $configuration->getTitle() . ': Mail test successful.';
$mailer->message = 'It works on my machine. 🚀';
$result = $mailer->send();

$response->setStatusCode(Response::HTTP_OK);
$response->setData(['success' => $result]);
} catch (Exception | TransportExceptionInterface $e) {
$response->setStatusCode(Response::HTTP_BAD_REQUEST);
$response->setData(['error' => $e->getMessage()]);
}

return $response;
}
}

0 comments on commit 473d5ca

Please sign in to comment.