Viewing File: /usr/local/cpanel/base/frontend/jupiter/ls_web_cache_manager/core/Lsc/Context/UserContext.php

<?php

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

namespace LsUserPanel\Lsc\Context;

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

/**
 * UserContext is a singleton
 */
class UserContext
{

    /**
     * @var UserContextOption
     */
    protected $options;

    /**
     * @var string
     */
    protected $dataFile;

    /**
     * @var null|string
     */
    protected $flagContent;

    /**
     * @var null|string
     */
    protected $readmeContent;

    /**
     *
     * @var null|UserContext
     */
    protected static $instance;

    /**
     *
     * @param UserContextOption $contextOption
     *
     * @throws UserLSCMException  Thrown indirectly by
     *     Ls_WebCacheMgr_Util::getUserLSCMDataDir() call.
     */
    protected function __construct( UserContextOption $contextOption )
    {
        $this->options  = $contextOption;
        $this->dataFile =
            Ls_WebCacheMgr_Util::getUserLSCMDataDir() . '/lscm.data';
    }

    /**
     *
     * @return UserContextOption
     *
     * @throws UserLSCMException  Thrown indirectly by self::me() call.
     */
    public static function getOption()
    {
        return self::me()->options;
    }

    /**
     *
     * @param UserContextOption $contextOption
     *
     * @throws UserLSCMException  Thrown when self::$instance is not null.
     * @throws UserLSCMException  Thrown indirectly by "new self()" call.
     * @throws UserLSCMException  Thrown indirectly by UserLogger::Initialize()
     *     call.
     */
    public static function initialize( UserContextOption $contextOption )
    {
        if ( self::$instance != null ) {

            throw new UserLSCMException(
                'Context cannot be initialized twice.',
                UserLSCMException::E_PROGRAM
            );
        }

        self::$instance = new self($contextOption);
        UserLogger::Initialize($contextOption);
    }

    /**
     *
     * @return UserContext
     *
     * @throws UserLSCMException  Thrown when self::$instance is null.
     */
    protected static function me()
    {
        if ( self::$instance == null ) {

            throw new UserLSCMException(
                'Uninitialized context.',
                UserLSCMException::E_NON_FATAL
            );
        }

        return self::$instance;
    }

    /**
     *
     * @return string
     *
     * @throws UserLSCMException  Re-thrown when self::me() call throws an
     *     UserLSCMException exception.
     * @throws UserLSCMException  Thrown indirectly by UserLogger::logMsg()
     *     call.
     */
    public static function getUserLSCMDataFile()
    {
        try {
            return self::me()->dataFile;
        }
        catch ( UserLSCMException $e ) {
            $msg = "{$e->getMessage()} Could not get data file.";
            UserLogger::logMsg($msg, UserLogger::L_DEBUG);

            throw new UserLSCMException($msg);
        }
    }

    /**
     *
     * @return int
     *
     * @throws UserLSCMException  Thrown indirectly by self::me() call.
     */
    public static function getScanDepth()
    {
        return self::me()->options->getScanDepth();
    }

    /**
     *
     * @return string
     *
     * @throws UserLSCMException  Thrown indirectly by self::me() call.
     */
    public static function getFlagFileContent()
    {
        $m = self::me();

        if ( $m->flagContent == null ) {
            $m->flagContent = <<<CONTENT
This file was created by LiteSpeed Web Cache Manager

When this file exists, your LiteSpeed Cache plugin for WordPress will NOT be affected
by Mass Enable/Disable operations performed through LiteSpeed Web Cache Manager.

Please DO NOT ATTEMPT to remove this file unless you understand the above.

CONTENT;
        }

        return $m->flagContent;
    }

}
Back to Directory File Manager