* @copyright Copyright 2020-2023 © Teamwant Mateusz Szymański All right reserved * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * @category Teamwant * @package Teamwant */ namespace Teamwant\Prestashop17\Redis\Classes; use Db; use DbQuery; use Language; use Media; use Tab; use Teamwant_redis\OverrideSrc\OverrideSrcCachingType; use Teamwant_redis\OverrideSrc\OverrideSrcParametersFile; use Tools; use Translate; trait TeamwantRedis { public $prestashopVersion = 1760; protected $config_form = false; public function initRawTranslations() { $this->l("Can't write file %s, please check \"rw\" permissions on this file"); $this->l('Actions'); $this->l('Add server'); $this->l('Alias'); $this->l('Any record is not valid'); $this->l('Connection failed'); $this->l('Error!'); $this->l('Host'); $this->l('Password'); $this->l('Please enter required fields, Host and port'); $this->l('Please input any record'); $this->l('Port'); $this->l('Remove row'); $this->l('Save success.'); $this->l('Save'); $this->l('Scheme'); $this->l('Test connection'); $this->l('Url to healthcheck:'); $this->l('Username'); $this->l('Wrong method, allowed: %s'); $this->l('You need administrator rights to make changes on this page.'); $this->l('Are you sure to save the changes? The redis configuration has not been saved.'); $this->l('Server %d is duplicated'); $this->l('File %s must be writable.'); $this->l('File %s must be readable.'); $this->l('Can\'t create file %s.'); $this->l('File %s must be writable. Please change permissions on this file and reinstal Redis cache module.'); $this->l('File %s must be readable. Please change permissions on this file and reinstal Redis cache module.'); $this->l('Show More'); $this->l('Show Less'); $this->l('Index'); //1.3.0 $this->l('Payload is invalid.'); $this->l('Configuration is not valid'); $this->l('Field %s is required'); $this->l('Field %s must be true or false'); $this->l('Additional configuration'); $this->l('Use cache in admin panel?'); $this->l('Use prefix for keys?'); $this->l('Key prefix'); $this->l('Leave blank to generate automatically'); } public function validateFilePrivilagesForTeamwantRedisModule() { load_Teamwant_redis_OverrideSrc(); if (!is_writable(OverrideSrcCachingType::FILE)) { $this->adminDisplayWarning(self::staticModuleTranslate('File %s must be writable. Please change permissions on this file and reinstal Redis cache module.', [ OverrideSrcCachingType::FILE ])); } if (!is_readable(OverrideSrcCachingType::FILE)) { $this->adminDisplayWarning(self::staticModuleTranslate('File %s must be readable. Please change permissions on this file and reinstal Redis cache module.', [ OverrideSrcCachingType::FILE ])); } if (!is_writable(OverrideSrcParametersFile::FILE)) { $this->adminDisplayWarning(self::staticModuleTranslate('File %s must be writable. Please change permissions on this file and reinstal Redis cache module.', [ OverrideSrcParametersFile::FILE ])); } if (!is_readable(OverrideSrcParametersFile::FILE)) { $this->adminDisplayWarning(self::staticModuleTranslate('File %s must be readable. Please change permissions on this file and reinstal Redis cache module.', [ OverrideSrcParametersFile::FILE ])); } if (!file_exists(TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php')) { @file_put_contents(TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php', ''); } if (file_exists(TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php')) { if (!is_writable(TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php')) { $this->adminDisplayWarning(self::staticModuleTranslate('File %s must be writable.', [ TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php' ])); } if (!is_readable(TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php')) { $this->adminDisplayWarning(self::staticModuleTranslate('File %s must be readable.', [ TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php' ])); } } else { $this->adminDisplayWarning(self::staticModuleTranslate('Can\'t create file %s.', [ TEAMWANT_REDIS_ROOT_DIR . '/config/_RedisConfiguration.php' ])); } } public static function staticModuleTranslate($string, $sprintf = null) { $r = Translate::getModuleTranslation( 'teamwant_redis', $string, 'teamwant_redis' ); if (!$sprintf) { return $r; } if (is_array($sprintf)) { return vsprintf($r, $sprintf); } return sprintf($r, $sprintf); } public function install() { $this->createCustomOverride($this->prestashopVersion); //custom override presta files load_Teamwant_redis_OverrideSrc(); $out = parent::install() && $this->registerHook('backOfficeHeader') && $this->registerHook('actionClearCompileCache') ; //custom override presta files OverrideSrcCachingType::install(); $this->registerAdminControllers(); //tworzenie startowego pliku $this->createDefaultConfigFile(); return $out; } public function createCustomOverride($version) { if (!file_exists(TEAMWANT_REDIS_ROOT_DIR . '/override')) { mkdir(TEAMWANT_REDIS_ROOT_DIR . '/override'); $this->dirCopy( TEAMWANT_REDIS_ROOT_DIR . '/overrideVersions/' . $version . '/', TEAMWANT_REDIS_ROOT_DIR . '/override/' ); } } public function dirCopy(string $src, string $dest) { foreach (scandir($src) as $file) { if ($file === '.' || $file === '..') { continue; } $newFile = $src . '/' . $file; if (!is_readable($newFile)) { continue; } if (is_dir($newFile)) { if (!file_exists($dest . '/' . $file)) { mkdir($dest . '/' . $file); } $this->dirCopy($newFile . '/', $dest . '/' . $file . '/'); } else { copy($newFile, $dest . '/' . $file); } } } public function registerAdminControllers() { $sql = new DbQuery(); $sql->select('*'); $sql->from('tab', 't'); $sql->where('t.class_name = "AdminRedisConfiguration"'); $sql->orderBy('position'); if (!Db::getInstance()->executeS($sql)) { $tab = new Tab(); $tab->active = true; $tab->class_name = 'AdminRedisConfiguration'; $tab->id_parent = -1; $tab->module = $this->name; $tab->name = array(); foreach (Language::getLanguages(true) as $lang) { $tab->name[$lang['id_lang']] = 'AdminRedisConfiguration'; } $tab->add(); } } public function uninstall() { //custom override presta files load_Teamwant_redis_OverrideSrc(); OverrideSrcCachingType::uninstall(); OverrideSrcParametersFile::uninstall(); $this->uninstallAdminControllers(); $this->removeCustomOverride(); return parent::uninstall(); } public function uninstallAdminControllers() { return Db::getInstance()->query(" DELETE FROM `" . _DB_PREFIX_ . "tab` WHERE `module` = 'teamwant_redis' "); } public function removeCustomOverride() { if (file_exists(TEAMWANT_REDIS_ROOT_DIR . '/override')) { $this->deleteDirectory(TEAMWANT_REDIS_ROOT_DIR . '/override'); } } private function deleteDirectory($dir) { if (file_exists($dir)) { if (is_dir($dir)) { $files = scandir($dir); foreach ($files as $file) { if ($file === '.' || $file === '..') { continue; } $this->deleteDirectory($dir . '/' . $file); } rmdir($dir); } elseif (is_file($dir)) { unlink($dir); } } } public function enable($force_all = false) { $this->createCustomOverride($this->prestashopVersion); //custom override presta files load_Teamwant_redis_OverrideSrc(); OverrideSrcCachingType::install(); $this->registerHook('backOfficeHeader'); $this->registerHook('actionClearCompileCache'); $this->registerAdminControllers(); return parent::enable($force_all); } public function disable($force_all = false) { //custom override presta files load_Teamwant_redis_OverrideSrc(); OverrideSrcCachingType::uninstall(); OverrideSrcParametersFile::uninstall(); $this->uninstallAdminControllers(); $this->removeCustomOverride(); return parent::disable($force_all); } public function hookBackOfficeHeader() { //custom js variables Media::addJsDef([ 'token_AdminRedisConfiguration' => Tools::getAdminTokenLite('AdminRedisConfiguration'), ]); $controller = Tools::getValue('controller', ''); if ($controller === 'AdminPerformance') { //custom js variables Media::addJsDef([ 'tw_redis_lang_save_change_on_performance' => $this->l('Are you sure to save the changes? The redis configuration has not been saved.') ]); } $this->context->controller->addJquery(); //custom js $this->context->controller->addJs($this->_path . 'views/js/redis-admin.js'); //custom css $this->context->controller->addCSS($this->_path . 'views/css/redis-admin.css'); } private function createDefaultConfigFile() { $template_file = TEAMWANT_REDIS_ROOT_DIR . 'config/_RedisConfiguration.php'; if ( !file_exists($template_file) || (file_exists($template_file) && Tools::file_get_contents($template_file) == '') ) { $fileManager = new FileManager(); $validator = new Validator(); $payload = [ 'teamwant_redis_row' => [ [ 'host' => '127.0.0.1', 'port' => '6379', 'scheme' => 'tcp', ], ], 'twredis' => [ 'use_cache_admin' => false, 'prefix' => 'default', 'use_prefix' => false, ], ]; $items = $validator->validateAjaxProcessSaveConfigurationTable($payload); if ($items['result']) { $newConfig = json_encode( $items['items'], JSON_UNESCAPED_UNICODE ); $fileManager->saveConfigFile('_RedisConfiguration.php', $newConfig); } } return true; } public function hookActionClearCompileCache() { if ( defined('_PS_CACHE_ENABLED_') && _PS_CACHE_ENABLED_ && defined('_PS_CACHING_SYSTEM_') && _PS_CACHING_SYSTEM_ === 'Redis' ) { if (!class_exists(\Teamwant\Prestashop17\Redis\Classes\Cache\Redis::class)) { require_once _PS_MODULE_DIR_ . 'teamwant_redis/vendor/autoload.php'; } try { \Teamwant\Prestashop17\Redis\Classes\Cache\Redis::flushAllDb(); } catch (\Exception $e) { if (_PS_MODE_DEV_) { die('Warning: Failed to clear the Redis Cache. The error was: ' . $e->getMessage()); } } } } /** * Konfigurator "Cache config" bedzie dostepny na ta chwile dla php7.4 */ public static function getCacheConfiguration() { return false; } }