33/** *********************************************
44 * LiteSpeed Web Server Cache Manager
55 *
6- * @author Michael Alegre
7- * @copyright (c) 2018-2023 LiteSpeed Technologies, Inc.
6+ * @author Michael Alegre
7+ * @copyright 2018-2025 LiteSpeed Technologies, Inc.
88 * *******************************************
99 */
1010
1111namespace Lsc \Wp ;
1212
1313use Lsc \Wp \Context \Context ;
14- use LsUserPanel \Lsc \UserLSCMException ;
1514
1615class PluginVersion
1716{
@@ -28,10 +27,16 @@ class PluginVersion
2827 const DOWNLOAD_DIR = '/usr/src/litespeed-wp-plugin ' ;
2928
3029 /**
31- * @var string
30+ * @var string Old conf file name.
3231 */
3332 const LSCWP_DEFAULTS_INI_FILE_NAME = 'const.default.ini ' ;
3433
34+ /**
35+ * @since 1.17.5
36+ * @var string New conf file name.
37+ */
38+ const LSCWP_DEFAULTS_JSON_FILE_NAME = 'const.default.json ' ;
39+
3540 /**
3641 * @var string
3742 */
@@ -51,13 +56,13 @@ class PluginVersion
5156 * @deprecated 4.1.3 Will be removed in favor of $this->shortVersions.
5257 * @var string[]
5358 */
54- protected $ knownVersions = array () ;
59+ protected $ knownVersions = [] ;
5560
5661 /**
5762 * @since 4.1.3
5863 * @var string[]
5964 */
60- protected $ shortVersions = array () ;
65+ protected $ shortVersions = [] ;
6166
6267 /**
6368 * @var string[]
@@ -248,7 +253,8 @@ protected function createDownloadDir()
248253 protected function setVersionFiles ()
249254 {
250255 $ this ->versionFile = Context::LOCAL_PLUGIN_DIR . '/lscwp_versions_v2 ' ;
251- $ this ->activeFile = Context::LOCAL_PLUGIN_DIR . '/lscwp_active_version ' ;
256+ $ this ->activeFile =
257+ Context::LOCAL_PLUGIN_DIR . '/lscwp_active_version ' ;
252258 }
253259
254260 /**
@@ -263,7 +269,7 @@ protected function setVersionFiles()
263269 */
264270 protected function checkOldVersionFiles ()
265271 {
266- $ dataDir = Context::getLSCMDataDir ();
272+ $ dataDir = Context::getLSCMDataDir ();
267273 $ oldActiveFile = "$ dataDir/lscwp_active_version " ;
268274
269275 if ( file_exists ($ oldActiveFile ) ) {
@@ -319,9 +325,11 @@ public function setCurrentVersion()
319325 */
320326 private function getActiveVersion ()
321327 {
322- if ( file_exists ($ this ->activeFile )
323- && ($ content = file_get_contents ($ this ->activeFile )) ) {
324-
328+ if (
329+ file_exists ($ this ->activeFile )
330+ &&
331+ ($ content = file_get_contents ($ this ->activeFile ))
332+ ) {
325333 return trim ($ content );
326334 }
327335
@@ -386,13 +394,11 @@ protected function setAllowedVersions()
386394 );
387395 }
388396
389- $ matchFound = preg_match (
390- '/allowed\s{(.*)}/sU ' ,
391- trim ($ content ),
392- $ m
393- );
394-
395- if ( !$ matchFound || ($ list = trim ($ m [1 ])) == '' ) {
397+ if (
398+ !preg_match ('/allowed\s{(.*)}/sU ' , trim ($ content ), $ m )
399+ ||
400+ ($ list = trim ($ m [1 ])) == ''
401+ ) {
396402 throw new LSCMException (
397403 'LSCWP version list is empty. ' ,
398404 LSCMException::E_NON_FATAL
@@ -426,13 +432,11 @@ protected function setShortVersions()
426432 );
427433 }
428434
429- $ matchFound = preg_match (
430- '/short\s{(.*)}/sU ' ,
431- trim ($ content ),
432- $ m
433- );
434-
435- if ( !$ matchFound || ($ list = trim ($ m [1 ])) == '' ) {
435+ if (
436+ !preg_match ('/short\s{(.*)}/sU ' , trim ($ content ), $ m )
437+ ||
438+ ($ list = trim ($ m [1 ])) == ''
439+ ) {
436440 throw new LSCMException (
437441 'LSCWP version list is empty. ' ,
438442 LSCMException::E_NON_FATAL
@@ -498,16 +502,21 @@ public function setActiveVersion( $version, $init = false )
498502
499503 /**
500504 *
501- * @param bool $isforced
505+ * @param bool $isForced
502506 *
503507 * @throws LSCMException Thrown indirectly by Logger::info() call.
504508 */
505- protected function refreshVersionList ( $ isforced = false )
509+ protected function refreshVersionList ( $ isForced = false )
506510 {
507511 clearstatcache ();
508512
509- if ( $ isforced || !file_exists ($ this ->versionFile )
510- || (time () - filemtime ($ this ->versionFile )) > 86400 ) {
513+ if (
514+ $ isForced
515+ ||
516+ !file_exists ($ this ->versionFile )
517+ ||
518+ (time () - filemtime ($ this ->versionFile )) > 86400
519+ ) {
511520
512521 if ( !file_exists (Context::LOCAL_PLUGIN_DIR ) ) {
513522 $ this ->createDownloadDir ();
@@ -550,6 +559,34 @@ protected function filterVerList( $ver )
550559 return Util::betterVersionCompare ($ ver , '1.2.2 ' , '> ' );
551560 }
552561
562+ /**
563+ *
564+ * @since 1.17.5
565+ *
566+ * @param string $pluginDir The WordPress plugin directory.
567+ */
568+ public static function tryCopyLscwpDefaultConf ( $ pluginDir )
569+ {
570+ foreach (
571+ [
572+ self ::LSCWP_DEFAULTS_INI_FILE_NAME ,
573+ self ::LSCWP_DEFAULTS_JSON_FILE_NAME
574+ ]
575+ as
576+ $ customConfFileName
577+ ) {
578+ $ customConfFilePath =
579+ Context::LOCAL_PLUGIN_DIR . "/ $ customConfFileName " ;
580+
581+ if ( file_exists ($ customConfFilePath ) ) {
582+ copy (
583+ $ customConfFilePath ,
584+ "$ pluginDir/litespeed-cache/data/ $ customConfFileName "
585+ );
586+ }
587+ }
588+ }
589+
553590 /**
554591 * Checks the current installation for existing LSCWP plugin files and
555592 * copies them to the installation's plugins directory if not found.
@@ -596,7 +633,9 @@ public static function prepareUserInstall( $pluginDir, $version = '' )
596633
597634 exec (
598635 '/bin/cp --preserve=mode -rf '
599- . Context::LOCAL_PLUGIN_DIR . "/ $ version/ " . self ::PLUGIN_NAME
636+ . Context::LOCAL_PLUGIN_DIR
637+ . "/ $ version/ "
638+ . self ::PLUGIN_NAME
600639 . " $ pluginDir "
601640 );
602641
@@ -607,16 +646,7 @@ public static function prepareUserInstall( $pluginDir, $version = '' )
607646 );
608647 }
609648
610- $ customIni = Context::LOCAL_PLUGIN_DIR . '/ '
611- . self ::LSCWP_DEFAULTS_INI_FILE_NAME ;
612-
613- if ( file_exists ($ customIni ) ) {
614- copy (
615- $ customIni ,
616- "$ pluginDir/litespeed-cache/data/ "
617- . self ::LSCWP_DEFAULTS_INI_FILE_NAME
618- );
619- }
649+ self ::tryCopyLscwpDefaultConf ($ pluginDir );
620650
621651 Logger::debug (
622652 'Copied LSCache for WordPress plugin files into plugins directory '
@@ -634,15 +664,15 @@ public static function prepareUserInstall( $pluginDir, $version = '' )
634664 */
635665 protected function hasDownloadedVersion ( $ version )
636666 {
637- $ dir = Context::LOCAL_PLUGIN_DIR . "/ $ version " ;
667+ $ dir = Context::LOCAL_PLUGIN_DIR . "/ $ version " ;
638668 $ md5file = "$ dir/ " . self ::VER_MD5 ;
639- $ plugin = "$ dir/ " . self ::PLUGIN_NAME ;
669+ $ plugin = "$ dir/ " . self ::PLUGIN_NAME ;
640670
641671 if ( !file_exists ($ md5file ) || !is_dir ($ plugin ) ) {
642672 return false ;
643673 }
644674
645- return ( file_get_contents ($ md5file ) == Util::DirectoryMd5 ($ plugin ) );
675+ return (file_get_contents ($ md5file ) == Util::DirectoryMd5 ($ plugin ));
646676 }
647677
648678 /**
@@ -772,8 +802,7 @@ public static function retrieveTranslation( $locale, $pluginVer )
772802 exec (
773803 'wget -q --tries=1 --no-check-certificate '
774804 . 'https://downloads.wordpress.org/translation/plugin/ '
775- . "litespeed-cache/ $ pluginVer/ $ locale.zip "
776- . "-P $ translationDir " ,
805+ . "litespeed-cache/ $ pluginVer/ $ locale.zip -P $ translationDir " ,
777806 $ output ,
778807 $ return_var
779808 );
@@ -805,9 +834,11 @@ public static function removeTranslationZip( $locale, $pluginVer )
805834 Context::LOCAL_PLUGIN_DIR . "/ $ pluginVer/translations/ $ locale.zip "
806835 );
807836
808- $ realPathStart = substr ($ zipFile , 0 , strlen (Context::LOCAL_PLUGIN_DIR ));
809-
810- if ( $ realPathStart === Context::LOCAL_PLUGIN_DIR ) {
837+ if (
838+ substr ($ zipFile , 0 , strlen (Context::LOCAL_PLUGIN_DIR ))
839+ ===
840+ Context::LOCAL_PLUGIN_DIR
841+ ) {
811842 unlink ($ zipFile );
812843 }
813844 }
0 commit comments