Viewing File: /usr/local/cpanel/base/frontend/jupiter/ls_web_cache_manager/core/EcCertManageController.php

<?php

/** *********************************************
 * LiteSpeed Web Cache Management Plugin for cPanel
 *
 * @author    Michael Alegre
 * @copyright 2020-2023 LiteSpeed Technologies, Inc.
 * @since     2.1
 * *******************************************
 */

namespace LsUserPanel;

use LsUserPanel\Lsc\UserLogger;
use LsUserPanel\Lsc\UserLSCMException;

/**
 *
 * @since 2.1
 */
class EcCertManageController
{

    /**
     * @since 2.1
     * @var   int
     */
    const MGR_STEP_NONE = 0;

    /**
     * @since 2.1
     * @var   int
     */
    const MGR_STEP_UPDATE = 1;

    /**
     * @since 2.1
     * @var   int
     */
    const MGR_STEP_GEN_ALL = 2;

    /**
     * @since 2.1
     * @var   int
     */
    const MGR_STEP_REMOVE_ALL = 3;

    /**
     * @since 2.1
     * @var   EcCertSiteStorage
     */
    private $ecCertSiteStorage;

    /**
     * @since 2.1
     * @var   int
     */
    private $mgrStep;

    /**
     *
     * @since 2.1
     *
     * @param EcCertSiteStorage $ecCertSiteStorage
     * @param int               $mgrStep
     */
    public function __construct(
        EcCertSiteStorage $ecCertSiteStorage,
                          $mgrStep )
    {
        $this->ecCertSiteStorage = $ecCertSiteStorage;
        $this->mgrStep           = $mgrStep;
    }

    /**
     *
     * @since 2.1
     *
     * @return void|string[]
     */
    private function getCurrentAction()
    {
        $all_actions = array(
            'gen_single'    => EcCertSiteStorage::CMD_GEN_EC,
            'remove_single' => EcCertSiteStorage::CMD_REMOVE_EC,
            'gen_sel'       => EcCertSiteStorage::CMD_GEN_EC,
            'remove_sel'    => EcCertSiteStorage::CMD_REMOVE_EC,
        );

        foreach ( $all_actions as $act => $doAct ) {

            if ( Ls_WebCacheMgr_Util::get_request_var($act) !== null ) {
                return array( 'act_key' => $act, 'action' => $doAct );
            }
        }
    }

    /**
     *
     * @since 2.1
     *
     * @return int
     *
     * @throws UserLSCMException  Thrown indirectly by $this->checkUpdate()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by $this->checkGenAll()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by $this->checkRemoveAll()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by $this->doFormAction()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by $this->doSingleAction()
     *     call.
     */
    public function manageCacheOperations()
    {
        if ( $this->checkUpdate()
                || $this->checkGenAll()
                || $this->checkRemoveAll()
                || ($actionInfo = $this->getCurrentAction()) == NULL ) {

            return $this->mgrStep;
        }

        $actKey = $actionInfo['act_key'];
        $action = $actionInfo['action'];

        if ( strcmp(substr($actKey, -3), 'sel') == 0 ) {
            $this->doFormAction($action);
        }
        else {
            $serverName = Ls_WebCacheMgr_Util::get_request_var($actKey);
            $this->doSingleAction($action, $serverName);
        }

        return $this->mgrStep;
    }

    /**
     *
     * @since 2.1
     *
     * @return bool
     *
     * @throws UserLSCMException  Thrown indirectly by
     *     $this->ecCertSiteStorage->updateList() call.
     */
    private function checkUpdate()
    {
        if ( !Ls_WebCacheMgr_Util::get_request_var('update_list') ) {
            return false;
        }

        $this->mgrStep = self::MGR_STEP_UPDATE;
        $this->ecCertSiteStorage->updateList();

        return true;
    }

    /**
     *
     * @since 2.1
     *
     * @return bool
     *
     * @throws UserLSCMException  Thrown indirectly by
     *     $this->ecCertSiteStorage->doEcCertAction() call.
     */
    private function checkGenAll()
    {
        if ( !Ls_WebCacheMgr_Util::get_request_var('gen_all') ) {
            return false;
        }

        $this->mgrStep = self::MGR_STEP_GEN_ALL;

        $this->ecCertSiteStorage->doEcCertAction(
            EcCertSiteStorage::CMD_GEN_EC,
            $this->ecCertSiteStorage->getServerNames()
        );

        return true;
    }

    /**
     *
     * @since 2.1
     *
     * @return bool
     *
     * @throws UserLSCMException  Thrown indirectly by
     *     $this->ecCertSiteStorage->doEcCertAction() call.
     */
    private function checkRemoveAll()
    {
        if ( !Ls_WebCacheMgr_Util::get_request_var('remove_all') ) {
            return false;
        }

        $this->mgrStep = self::MGR_STEP_REMOVE_ALL;

        $this->ecCertSiteStorage->doEcCertAction(
            EcCertSiteStorage::CMD_REMOVE_EC,
            $this->ecCertSiteStorage->getServerNames()
        );

        return true;
    }

    /**
     *
     * @since 2.1
     *
     * @param string $action
     * @return void
     *
     * @throws UserLSCMException  Thrown indirectly by UserLogger::addUiMsg()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by UserLogger::addUiMsg()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by
     *     $this->ecCertSiteStorage->doEcCertAction() call.
     */
    private function doFormAction( $action )
    {
        $list = Ls_WebCacheMgr_Util::get_request_list('domains');

        if ( $list == NULL ) {
            UserLogger::addUiMsg(
                _('Please select at least one checkbox.'),
                UserLogger::UI_ERR
            );
            return;
        }

        foreach ( $list as $domain ) {

            if ( $this->ecCertSiteStorage->getEcCertSite($domain) === null ) {
                UserLogger::addUiMsg(
                    _('Invalid input value detected - No Action Taken'),
                    UserLogger::UI_ERR
                );
                return;
            }
        }

        $this->ecCertSiteStorage->doEcCertAction($action, $list);
    }

    /**
     *
     * @since 2.1
     *
     * @param string $action
     * @param string $serverName
     * @return void
     *
     * @throws UserLSCMException  Thrown indirectly by UserLogger::addUiMsg()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by
     *     $this->ecCertSiteStorage->doEcCertAction() call.
     */
    private function doSingleAction( $action, $serverName )
    {
        if ( $this->ecCertSiteStorage->getEcCertSite($serverName) === null ) {
            UserLogger::addUiMsg(
                _('Invalid input value detected - No Action Taken'),
                UserLogger::UI_ERR
            );
            return;
        }

        $this->ecCertSiteStorage->doEcCertAction($action, array( $serverName ));
    }

}
Back to Directory File Manager