Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/ncssl/source/src/Controller/InstalledListController.php

<?php

namespace App\Controller;

use App\Entity\User;
use App\Exception\UserException;
use App\Model\GetAutoToggleRequestDTO;
use App\Model\GetToggleRequestDTO;
use App\Service\Certificate\Certificate;
use App\Service\User as UserService;
use App\Service\Certificate\SyncCertificate;
use App\Traits\RouteGeneratorTrait;
use CPANEL;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use Psr\Log\LoggerInterface;
use Symfony\Bridge\Twig\Attribute\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Attribute\CurrentUser;

class InstalledListController extends AbstractController
{
    use RouteGeneratorTrait;

    #[Route('ssls/installed', name: 'list.installed')]
    #[Template('sslList/installed.html.twig')]
    public function index(
        #[CurrentUser] ?User $user,
        #[MapQueryParameter] ?int $withoutSync,
        Certificate $certificateService,
        SyncCertificate $syncCertificate,
        LoggerInterface $logger
    ): array | RedirectResponse
    {
        $currentUser = $user?->getNcLogin();
        if (!$currentUser) {
            return $this->redirectToRoute('home');
        }

        if (!$withoutSync) {
            try {
                $syncCertificate->syncForLocalDbUpdate();
            } catch (\Exception $e) {
                $logger->error('Sync certificates failed', ['exception' => $e]);
            }
        }

        $certificates = $certificateService->getInstalledCertificates();

        return [
            'certificates' => $certificates['certificates'],
            'certsGrouped' => $certificates['certsGrouped'],
            'freeCertificatesInfo' => $certificates['freeCertificatesInfo']
        ];
    }

    #[Route('ssls/toggle_redirect', name: 'toggle-http-redirect', methods: ['GET'])]
    public function toggleHttpRedirect(
        #[MapQueryString] GetToggleRequestDTO $toggleRequestDTO,
        #[CurrentUser] ?User $user,
        Certificate $certificateService,
        #[Autowire(service: 'cpanel')] \CPANEL $cpanel
    ): JsonResponse | null
    {
        $currentUser = $user?->getNcLogin();
        if (!$currentUser) {
            return null;
        }

        $domain = $toggleRequestDTO->domain;
        $expectedStatus = (bool)$toggleRequestDTO->expectedStatus;
        $certificateId = $toggleRequestDTO->certId;

        $result = $certificateService->updateToggle($certificateId, $expectedStatus, $domain);
        $cpanel->get_debug_level(); // added fox fix problem with error "Child failed to make LIVEAPI connection to cPanel."

        return new JsonResponse($result);
    }

    #[Route('ssls/toggle_redirect_auto_general', name: 'auto-toggle-http-redirect', methods: ['GET'])]
    public function autoToggleHttpRedirect(
        #[MapQueryString] GetAutoToggleRequestDTO $autoToggleRequestDTO,
        #[CurrentUser] ?User $user,
        UserService $userService,
        LoggerInterface $logger,
        #[Autowire(service: 'cpanel')] \CPANEL $cpanel
    ): JsonResponse | null
    {
        $currentUser = $user?->getNcLogin();
        if (!$currentUser) {
            return null;
        }
        $userName = $user->getName();
        $cpanel->get_debug_level(); // added fox fix problem with error "Child failed to make LIVEAPI connection to cPanel."

        $ncUserLogin = $autoToggleRequestDTO->user;
        $expectedStatus = (bool)$autoToggleRequestDTO->expectedStatus;

        try {
            $userService->setAutoRedirect($ncUserLogin, $userName, $expectedStatus);
        } catch (ORMException | OptimisticLockException | UserException $e) {
            $logger->error('Error while updating auto redirect status', ['exception' => $e]);

            return new JsonResponse([
                    'status' => Certificate::STATUS_FAIL,
                    'message'=> $e->getMessage(),
            ]);
        }

        return new JsonResponse([
                'status' => Certificate::STATUS_SUCCESS,
                'message'=> '',
        ]);
    }
}
Back to Directory File Manager