Edit file File name : class.A2OptTurboCache.php Content :<?php class A2OptTurboCache extends A2OptBase { public $action; public $app; public $home; public $user; public $htaccess; public $domains; public $config; public function __construct() { parent::__construct(); $this->action = $_REQUEST['action']; if (!is_dir($this->app->path) || !is_writable($this->app->path)) { $this->error("Application directory {$this->app->path} is not writable."); } $this->config = $this->app->swiftcache->turbocache; $this->htaccess = "{$this->app->path}/.htaccess"; } public function apply_action() { $this->log_action('TurboCache: apply_action'); switch ($this->action) { case 'enable': $this->log_action('TurboCache: apply_action enable'); $apply_htaccess = true; if (!isset($this->app->swiftcache) || !isset($this->app->swiftcache->turbocache)) { $this->error('Missing TurboCache info.'); } if ($this->app->type == 'PrestaShop') { $this->app->ps_ver = $this->get_prestashop_version($this->app->path); $prestashop = new A2OptPrestaShop($this->app); $prestashop->install_module(); if ($this->config->enabled == false) { // Set defaults for first enable $defaults = $prestashop->get_turbocache_defaults(); $this->config->reject_urls = $defaults['reject_urls']; $this->config->reject_cookies = $defaults['reject_cookies']; $this->config->time = $defaults['time']; } } if ($this->app->type == 'OpenCart') { $oc = new A2OptOpenCart($this->app); $oc->install_extension(__DIR__ . '/../../opencart/Cookies.xml'); } if ($this->app->type == 'WordPress') { $wp = new A2OptWordPress($this->app); $wp->wp_cli('plugin install litespeed-cache --activate'); $wp->wp_cli('a2-optimized page_cache disable'); $apply_htaccess = false; } if ($this->app->type == 'Drupal' || $this->app->type == 'Drupal 9') { $this->app->dr_ver = $this->get_drupal_version($this->app->path); if ($this->app->dr_ver == 8 || $this->app_dr_ver == 9) { // Use lscache module $apply_htaccess = false; // overwrite existing files, quiet mode $this->su_exec('unzip -oq /opt/a2-optimized/drupal/lscache-drupal.zip -d ' . $this->app->path . '/modules'); // enable module $this->su_exec('/opt/a2-optimized/drupal/drush8 en lite_speed_cache -y', $this->app->path); } else { if ($this->config->enabled == false) { // Set defaults for first enable $drupal = $this->get_drupal_object($this->app); $defaults = $drupal->get_turbocache_defaults(); $this->config->reject_urls = $defaults['reject_urls']; $this->config->reject_cookies = $defaults['reject_cookies']; $this->config->time = $defaults['time']; } } } if ($apply_htaccess) { $this->write_htaccess(); } $this->app->swiftcache->turbocache->enabled = true; break; case 'disable': if ($this->app->type == 'WordPress') { $wp = new A2OptWordPress($this->app); $wp->wp_cli('plugin deactivate litespeed-cache'); } if ($this->app->type == 'Drupal' || $this->app->type == 'Drupal 9') { $this->app->dr_ver = $this->get_drupal_version($this->app->path); if ($this->app->dr_ver == 8 || $this->app_dr_ver == 9) { // disable module $this->su_exec('/opt/a2-optimized/drupal/drush8 pm-uninstall lite_speed_cache -y', $this->app->path); } } $this->remove_htaccess(); $this->app->swiftcache->turbocache->enabled = false; break; case 'save': if (!isset($this->app->swiftcache) || !isset($this->app->swiftcache->turbocache)) { $this->error('Missing TurboCache info.'); } $this->write_htaccess(); break; case 'purge': $this->purge(); break; case 'read_config': break; case 'get_defaults': $this->get_defaults(); if (is_array($this->app->swiftcache->turbocache)) { $this->app->swiftcache->turbocache = (object) $this->app->swiftcache->turbocache; } break; default: $this->error('No action was given'); } if ($this->action != 'get_defaults') { $this->read_config(); } if (in_array($this->action, ['enable', 'disable', 'save'])) { $app_db = ORM::for_table('apps')->where('app_path', $this->app->path)->find_one(); if ($app_db) { $app_db->app_swiftcache_turbocache = json_encode($this->app->swiftcache->turbocache); $app_db->save(); } } echo json_encode($this->app); } private function get_defaults() { $app = null; switch ($this->app->type) { case 'PrestaShop': $this->app->ps_ver = $this->get_prestashop_version($this->app->path); $app = new A2OptPrestaShop($this->app); break; case 'WordPress': $app = new A2OptWordPress($this->app); break; case 'Magento': $app = new A2OptMagento($this->app); break; case 'Drupal': case 'Drupal 9': $this->app->dr_ver = $this->get_drupal_version($this->app->path); $app = $this->get_drupal_object($this->app); break; case 'Joomla': $app = new A2OptJoomla($this->app); break; case 'OpenCart': $app = new A2OptOpenCart($this->app); break; default: $this->error("{$this->app->type} is not supported by A2 Optimized"); } if ($app != null) { $this->config = $app->get_turbocache_defaults(); $this->app->swiftcache->turbocache = $this->config; } else { $this->error("{$this->app->type} is not configured properly"); } } public function error($msg, $code = '400') { header("HTTP/1.1 $code Bad Request"); echo json_encode((object) ['error' => $msg]); exit(1); } public function remove_htaccess() { if (!copy($this->htaccess, "{$this->htaccess}.lscache.bak")) { if (!is_writable($this->app->path)) { $this->error("The web server does not have write access to {$this->app->path}"); } else { touch($this->htaccess); } } $htaccess_contents = file_get_contents($this->htaccess);//read current file contents $pattern = "/[\r\n]*# START A2 LiteSpeed Cache.*# END A2 LiteSpeed Cache.*[\r\n]+/msiU"; $htaccess_contents = preg_replace($pattern, '', $htaccess_contents); // remove any prior lscache rules if ($fp = fopen($this->htaccess, 'w+')) { fwrite($fp, $htaccess_contents); fclose($fp); } else { $this->error("The web server does not have write access to {$this->htaccess}"); } } public function sanitize($string) { return str_replace([' ',"\t",'..','*'], '', $string); } public function write_htaccess() { $this->log_action('TurboCache: write_htaccess this->config: ' . json_encode($this->config)); if ( !( isset( $this->config->reject_urls ) && isset( $this->config->reject_cookies ) && isset( $this->config->time ) ) ) { $this->log_action('TurboCache: write_htaccess: missing turbocache config'); $this->error('Missing TurboCache Config Settings'); } //// SET UP HTACCESS ///////////////////////////////////////////////////////////////////////////////// if (file_exists($this->htaccess)) { // backup copy if (!copy($this->htaccess, "{$this->htaccess}.lscache.bak")) { if (!is_writable($this->app->path)) { $this->error("The web server does not have write access to {$this->app->path}"); } else { touch($this->htaccess); } } $htaccess_contents = file_get_contents($this->htaccess);//read current file contents $pattern = "/[\r\n]*# START A2 LiteSpeed Cache for .*# END A2 LiteSpeed Cache for .*[\r\n]+/msiU"; $htaccess_contents = preg_replace($pattern, '', $htaccess_contents); // remove any prior lscache rules } else {//.htaccess file does not exist $htaccess_contents = ''; } $rejected_urls = trim(implode('|', $this->config->reject_urls), '|'); $rejected_cookies = trim(implode('|', $this->config->reject_cookies), '|'); //make sure that there is something on the line for a placeholder $request_uri = !empty($rejected_urls) ? "RewriteCond %{REQUEST_URI} !{$rejected_urls} [NC]" : '#RewriteCond %{REQUEST_URI} !admin'; $http_cookie = !empty($rejected_cookies) ? "RewriteCond %{HTTP_COOKIE} !{$rejected_cookies} [NC]" : '#RewriteCond %{HTTP_COOKIE} !login|logged'; $domains = implode('|', $this->app->domains); $ttl = $this->ttl2sec($this->config->time); $lscache_rules = <<<RULES # START A2 LiteSpeed Cache for {$this->app->title} # Type: {$this->app->type} <IfModule LiteSpeed> CacheEnable public RewriteEngine On RewriteCond %{REQUEST_METHOD} ^GET|HEAD|PURGE$ RewriteCond %{HTTP_HOST} {$domains} [NC] {$request_uri} {$http_cookie} RewriteCond %{QUERY_STRING} !nocache [NC] RewriteRule .* - [E=Cache-Control:max-age={$ttl}] </IfModule> # END A2 LiteSpeed Cache for {$this->app->title} RULES; $htaccess_contents = "{$lscache_rules}{$htaccess_contents}"; if ($fp = fopen($this->htaccess, 'w+')) { fwrite($fp, $htaccess_contents); fclose($fp); } else { $this->error("The web server does not have write access to {$this->htaccess}"); } } public function ttl2sec($time) { switch ($time) {//returns ttl in seconds from time in minutes/hours/days/weeks case '1 Minute': return 60; break; case '2 Minutes': return 120; break; case '3 Minutes': return 180; break; case '5 Minutes': return 300; break; case '15 Minutes': return 900; break; case '30 Minutes': return 1800; break; case '1 Hour': return 3600; break; case '2 Hours': return 7200; break; case '6 Hours': return 21600; break; case '12 Hours': return 43200; break; case '24 Hours': return 86400; break; case '2 Days': return 172800; break; case '7 Days': return 604800; break; case '2 Weeks': return 1209600; break; default: return 60; } } public function sec2ttl($ttl) { $ttl = "{$ttl}"; switch ($ttl) {//returns ttl in seconds from time in minutes/hours/days/weeks case '60': return '1 Minute'; break; case '120': return '2 Minutes'; break; case '180': return '3 Minutes'; break; case '300': return '5 Minutes'; break; case '900': return '15 Minutes'; break; case '1800': return '30 Minutes'; break; case '3600': return '1 Hour'; break; case '7200': return '2 Hours'; break; case '21600': return '6 Hours'; break; case '43200': return '12 Hours'; break; case '86400': return '24 Hours'; break; case '172800': return '2 Days'; break; case '604800': return '7 Days'; break; case '1209600': return '2 Weeks'; break; default: return '1 Minute'; } } public function purge() { $dir = "{$this->home}/lscache"; if (is_dir($dir)) {// make sure it is a directory and that it is writable if (is_writable($dir)) { if ($this->sanitize($dir) === $dir && $dir != '') {//strip out special chars and make sure it still matches $shell_string = $this->su_exec("rm -rf {$dir}/* 1>/dev/null"); if ($shell_string != '') { $this->error('An error occurred while deleting the cache directories.'); } else { return true; } } else { $this->error('The directory path for the cache has been modified and is not accessible.'); } } else { $this-> error("Write privelages for {$dir} have been modified and it is not accessible."); } } foreach ($this->app->domains as $i => $domain) { $dir = "{$this->home}/turbocache/" . $this->sanitize($domain); if (is_dir($dir)) {// make sure it is a directory and that it is writable if (is_writable($dir)) { if ($this->sanitize($dir) === $dir && $dir != '') {//strip out special chars and make sure it still matches $shell_string = $this->su_exec("rm -rf {$dir}/* 1>/dev/null"); if ($shell_string != '') { $this->error('An error occurred while deleting the cache directories for ' . $domain); } } else { $this->error('The directory path for the cache has been modified and is not accessible for ' . $domain); } } else { $this-> error("Write privelages for {$dir} have been modified and it is not accessible."); } } } return true; } public function read_config() { if (!$this->app->swiftcache || !$this->app->swiftcache->turbocache) { $this->app->swiftcache = new stdClass(); $this->app->swiftcache->turbocache = new stdClass(); } if (is_array($this->app->swiftcache->turbocache)) { $this->app->swiftcache->turbocache = (object) $this->app->swiftcache->turbocache; } $htaccess_contents = file_get_contents($this->htaccess);//read current file contents $pattern = "/[\r\n]*# START A2 LiteSpeed Cache for .*# END A2 LiteSpeed Cache for .*[\r\n]*/msiU"; if ($htaccess_contents = preg_match($pattern, $htaccess_contents, $matches) && isset($this->app->swiftcache->turbocache)) { // find any lscache rules $lscache = array_pop($matches); $reject_urls = []; if (preg_match('/[^#]RewriteCond %{REQUEST_URI} !(.*) \[NC\]/', $lscache, $reject_urls)) { $reject_urls = explode('|', array_pop($reject_urls)); } $reject_cookies = []; if (preg_match('/[^#]RewriteCond %{HTTP_COOKIE} !(.*) \[NC\]/', $lscache, $reject_cookies)) { $reject_cookies = explode('|', array_pop($reject_cookies)); } $ttl = 60; if (preg_match('/RewriteRule \.\* - \[E=Cache-Control:max-age=([0-9]*)\]/', $lscache, $ttl_match)) { $ttl = array_pop($ttl_match); } $this->app->swiftcache->turbocache->reject_urls = $reject_urls; $this->app->swiftcache->turbocache->reject_cookies = $reject_cookies; $this->app->swiftcache->turbocache->ttl = $ttl; $this->app->swiftcache->turbocache->time = $this->sec2ttl("{$ttl}"); if ($this->app->swiftcache->turbocache->ttl != 0 && $this->app->swiftcache->turbocache->ttl != '0') { $this->app->swiftcache->turbocache->enabled = true; } } else { if (isset($this->app->swiftcache) && isset($this->app->swiftcache->turbocache)) { $this->app->swiftcache->turbocache->enabled = false; } } // Alternate ways of checking for Turbo / LiteSpeed if ($this->app->type == 'Drupal' || $this->app->type == 'Drupal 9') { $this->app->dr_ver = $this->get_drupal_version($this->app->path); if ($this->app->dr_ver == 8 || $this->app_dr_ver == 9) { $drupal = $this->get_drupal_object($this->app); if ($drupal->is_litespeed_enabled()) { $this->app->swiftcache->turbocache->enabled = true; } } } if ($this->app->type == 'WordPress') { $wp = new A2OptWordPress($this->app); if ($wp->is_litespeed_enabled()) { $this->app->swiftcache->turbocache->enabled = true; } } } } Save