Viewing File: /usr/local/cpanel/base/frontend/jupiter/ls_web_cache_manager/core/View/Model/MainViewModel.php

<?php

/** ******************************************
 * LiteSpeed Web Cache Management Plugin for cPanel
 *
 * @author    Michael Alegre
 * @copyright 2018-2025 LiteSpeed Technologies, Inc.
 * ******************************************* */

namespace LsUserPanel\View\Model;

use LsUserPanel\Ls_WebCacheMgr_Controller;
use LsUserPanel\Ls_WebCacheMgr_Util as Util;
use LsUserPanel\Lsc\Context\UserContext;
use LsUserPanel\Lsc\UserLogger;
use LsUserPanel\Lsc\UserLSCMException;
use LsUserPanel\Lsc\UserUtil;
use LsUserPanel\PluginSettings;

class MainViewModel
{

    /**
     * @var string
     */
    const FLD_PLUGIN_VER = 'pluginVer';

    /**
     * @var string
     */
    const FLD_VH_CACHE_DIR = 'vhCacheDir';

    /**
     * @var string
     */
    const FLD_VH_CACHE_DIR_EXISTS = 'vhCacheDirExists';

    /**
     * @var string
     */
    const FLD_ICON_DIR = 'iconDir';

    /**
     * @since 2.1
     * @var   string
     */
    const FLD_EC_ALLOWED = 'ecAllowed';

    /**
     * @since 2.3
     * @var   string
     */
    const FLD_CAGE_TYPE = 'cageType';

    /**
     * @since 2.3
     * @var   string
     */
    const FLD_CAGED_REDIS_MSG = 'cagedRedisMsg';

    /**
     * @since 2.3
     * @var   string
     */
    const FLD_REDIS_ONLY = 'redisOnly';

    /**
     * @since 2.3
     * @var   string
     */
    const FLD_REDIS_ENABLED = 'redisEnabled';

    /**
     * @var string
     */
    const FLD_ERR_MSGS = 'errMsgs';

    /**
     * @var string
     */
    const FLD_SUCC_MSGS = 'succMsgs';

    /**
     * @var array
     */
    private array $tplData = [];

    /**
     *
     * @param int $redisOnly
     *
     * @throws UserLSCMException  Thrown indirectly by $this->init() call.
     */
    public function __construct( int $redisOnly )
    {
        $this->init($redisOnly);
    }

    /**
     *
     * @param int $redisOnly
     *
     * @throws UserLSCMException  Thrown indirectly by UserLogger::debug() call.
     * @throws UserLSCMException  Thrown indirectly by
     *     $this->setVhCacheDirData() call.
     * @throws UserLSCMException  Thrown indirectly by $this->setIconDirData()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by $this->setEcCertAllowed()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by $this->setRedisInfo()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by $this->setMsgData() call.
     */
    private function init( int $redisOnly ): void
    {
        UserLogger::debug("Request URI: {$_SERVER['REQUEST_URI']}");

        $this->tplData[self::FLD_REDIS_ONLY] = $redisOnly;

        $this->setPluginVerData();
        $this->setVhCacheDirData();
        $this->setIconDirData();
        $this->setEcCertAllowed();
        $this->setRedisInfo();
        $this->setMsgData();
    }

    /**
     *
     * @param string $field
     *
     * @return mixed|null
     */
    public function getTplData( string $field )
    {
        return $this->tplData[$field] ?? null;
    }

    private function setPluginVerData()
    {
        $this->tplData[self::FLD_PLUGIN_VER] =
            Ls_WebCacheMgr_Controller::MODULE_VERSION;
    }

    /**
     *
     * @throws UserLSCMException  Thrown indirectly by
     *     UserUtil::getUserCacheDir() call.
     */
    private function setVhCacheDirData(): void
    {
        $cacheDir = UserUtil::getUserCacheDir();

        $this->tplData[self::FLD_VH_CACHE_DIR]        = $cacheDir;
        $this->tplData[self::FLD_VH_CACHE_DIR_EXISTS] =
            ($cacheDir != '' && file_exists($cacheDir));
    }

    /**
     *
     * @throws UserLSCMException  Thrown indirectly by
     *     UserContext::getOption() call.
     * @throws UserLSCMException  Thrown indirectly by UserLogger::logMsg()
     *     call.
     */
    private function setIconDirData(): void
    {
        $iconDir = '';

        try {
            $iconDir = UserContext::getOption()->getIconDir();
        }
        catch ( UserLSCMException $e ) {
            UserLogger::logMsg(
                "{$e->getMessage()} Could not get icon directory.",
                UserLogger::L_DEBUG
            );
        }

        $this->tplData[self::FLD_ICON_DIR] = $iconDir;
    }

    /**
     *
     * @since 2.1
     *
     * @throws UserLSCMException  Thrown indirectly by
     *     PluginSettings::getSetting() call.
     */
    private function setEcCertAllowed(): void
    {
        $this->tplData[self::FLD_EC_ALLOWED] = (
            PluginSettings::getSetting(PluginSettings::FLD_GENERATE_EC_CERTS)
            !=
            PluginSettings::SETTING_OFF
        );
    }

    /**
     *
     * @since 2.3
     *
     * @throws UserLSCMException  Thrown indirectly by Util::getRedisDbSize()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by Util::getCageType() call.
     * @throws UserLSCMException  Thrown indirectly by Util::cagedRedisRunning()
     *     call.
     */
    private function setRedisInfo(): void
    {
        $redisEnabled  = false;
        $cagedRedisMsg = '';

        $this->tplData[self::FLD_CAGE_TYPE] = $cageType = Util::getCageType();

        if (
                Util::getRedisDbSize(Util::getCurrentCpanelUser())
                &&
                $cageType > 0
        ) {

            if ( Util::cagedRedisRunning() ) {
                $redisEnabled  = true;
                $cagedRedisMsg = sprintf(_('Disable %s Service'), 'Redis');
            } else {
                $cagedRedisMsg = sprintf(_('Enable %s Service'), 'Redis');
            }
        }

        $this->tplData[self::FLD_REDIS_ENABLED]   = $redisEnabled;
        $this->tplData[self::FLD_CAGED_REDIS_MSG] = $cagedRedisMsg;
    }

    /**
     *
     * @throws UserLSCMException  Thrown indirectly by UserLogger::getUiMsgs()
     *     call.
     * @throws UserLSCMException  Thrown indirectly by UserLogger::getUiMsgs()
     *     call.
     */
    private function setMsgData(): void
    {
        $this->tplData[self::FLD_ERR_MSGS]  =
            UserLogger::getUiMsgs(UserLogger::UI_ERR);
        $this->tplData[self::FLD_SUCC_MSGS] =
            UserLogger::getUiMsgs(UserLogger::UI_SUCC);
    }

    /**
     *
     * @return string
     */
    public function getTpl(): string
    {
        return realpath(__DIR__ . '/../Tpl') . '/Main.tpl';
    }

}
Back to Directory File Manager