Viewing File: /usr/local/cpanel/base/frontend/jupiter/ls_web_cache_manager/core/UserSettings.php
<?php
/** ******************************************
* LiteSpeed Web Cache Management Plugin for cPanel
*
* @author Michael Alegre
* @copyright 2018-2023 LiteSpeed Technologies, Inc.
* ******************************************* */
namespace LsUserPanel;
use LsUserPanel\Lsc\UserLogger;
use LsUserPanel\Lsc\UserLSCMException;
class UserSettings
{
/**
* @var string
*/
const FLD_LOG_FILE_LVL = 'logFileLvl';
/**
* @var null|UserSettings
*/
private static $instance;
/**
* @var string
*/
private $settingsFile;
/**
* @var array
*/
private $settings = array();
/**
*
* @throws UserLSCMException Thrown indirectly by $this->init() call.
*/
private function __construct()
{
$this->init();
}
/**
*
* @throws UserLSCMException Thrown indirectly by
* Ls_WebCacheMgr_Util::getUserLSCMDataDir() call.
* @throws UserLSCMException Thrown indirectly by
* Ls_WebCacheMgr_Util::createUserLSCMDataDir() call.
*/
private function init()
{
$this->setDefaultSettings();
$dataDir = Ls_WebCacheMgr_Util::getUserLSCMDataDir();
if ( !file_exists($dataDir) ) {
Ls_WebCacheMgr_Util::createUserLSCMDataDir();
}
$this->settingsFile = "$dataDir/settings";
if ( !file_exists($this->settingsFile) ) {
$this->writeSettingsFile();
}
else {
$this->readSettingsFile();
}
}
/**
*
* @throws UserLSCMException Thrown when UserSettings object has already
* been initialized.
* @throws UserLSCMException Thrown indirectly by "new self()" call.
*/
public static function initialize()
{
if ( self::$instance != null ) {
/**
* Do not allow, already initialized.
*/
throw new UserLSCMException(
'UserSettings cannot be initialized twice.',
UserLSCMException::E_PROGRAM
);
}
self::$instance = new self();
}
/**
*
* @return UserSettings
*
* @throws UserLSCMException Thrown when UserSettings object has not been
* initialized yet.
*/
private static function me()
{
if ( self::$instance == null ) {
/**
* Do not allow, must initialize first.
*/
throw new UserLSCMException('Uninitialized UserSettings.');
}
return self::$instance;
}
/**
*
* @param string $setting
*
* @return mixed
*
* @throws UserLSCMException Thrown indirectly by self::me() call.
*/
public static function getSetting( $setting = '' )
{
$m = self::me();
if ( !isset($m->settings[$setting]) ) {
return null;
}
return $m->settings[$setting];
}
private function setDefaultSettings()
{
$this->settings = array( self::FLD_LOG_FILE_LVL => UserLogger::L_INFO );
}
/**
*
* @param array $settings
*
* @throws UserLSCMException Thrown indirectly by self::me() call.
* @throws UserLSCMException Thrown indirectly by $m->setLogFileLvl() call.
* @throws UserLSCMException Thrown indirectly by UserLogger::addUiMsg()
* call.
*/
public static function setSettings( array $settings )
{
$m = self::me();
if ( $m->setLogFileLvl($settings[self::FLD_LOG_FILE_LVL])
&& $m->writeSettingsFile() ) {
UserLogger::addUiMsg(
_('Successfully saved user settings.'),
UserLogger::UI_SUCC
);
}
}
/**
*
* @param int $lvl
*
* @return bool
*
* @throws UserLSCMException Thrown indirectly by UserLogger::addUiMsg()
* call.
*/
private function setLogFileLvl( $lvl )
{
if ( $this->isValidLogFileLvl($lvl) ) {
$lvl = min($lvl, UserLogger::L_DEBUG);
if ( $lvl != $this->settings[self::FLD_LOG_FILE_LVL] ) {
$this->settings[self::FLD_LOG_FILE_LVL] = $lvl;
return true;
}
}
else {
UserLogger::addUiMsg(
_('Could not set Log File Level'),
UserLogger::UI_ERR
);
}
return false;
}
/**
*
* @param string $pattern
* @param string $contents
*
* @return string
*/
private function readSetting( $pattern, $contents )
{
preg_match($pattern, $contents, $matches);
if ( !isset($matches[1]) ) {
return '';
}
return $matches[1];
}
private function readSettingsFile()
{
$logFileLvl = $this->readSetting(
'/LOG_FILE_LVL = (\d+)/',
file_get_contents($this->settingsFile)
);
if ( $logFileLvl !== '' ) {
$logFileLvl = (int)$logFileLvl;
if ( $this->isValidLogFileLvl($logFileLvl) ) {
$this->settings[self::FLD_LOG_FILE_LVL] =
min($logFileLvl, UserLogger::L_DEBUG);
}
}
}
/**
*
* @return bool
*/
private function writeSettingsFile()
{
$content = <<<EOF
LOG_FILE_LVL = {$this->settings[self::FLD_LOG_FILE_LVL]}
EOF;
if ( file_put_contents($this->settingsFile, $content) === false ) {
return false;
}
return true;
}
/**
*
* @param int $lvl
*
* @return bool
*/
private function isValidLogFileLvl( $lvl )
{
if ( !is_int($lvl) || $lvl < 0 ) {
return false;
}
return true;
}
}
Back to Directory
File Manager