JFIF$        dd7 

Viewing File: /opt/cloudlinux/venv/lib/python3.11/site-packages/xray/adviser/wordpress_plugin_manager.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2021 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

import logging
import os
import configparser
import shutil
import json
from xray import gettext as _


class Plugin:
    """
    Helper class which hides differences of WordPress plugins behind abstract methods.
    """

    NAME = ''
    SOURCE_DIR = ''
    INFO_FILE_PATH = ''
    ZIP_FILE_PATH = ''
    PLUGIN_DATA = None


    def _get_version(self):
        """Get the plugin version from plugin data"""
        if (plugin_data := self._get_data_dict()) is None:
            return None

        for section in plugin_data:
            if 'version' in plugin_data[section]:
                return plugin_data[section]['version']

        logging.error('Can\'t get the %s plugin version.', self.NAME)
        return None


    def _read_ini_file(self):
        """Read the ini file"""
        config_object = configparser.ConfigParser()
        config_object.read(self.INFO_FILE_PATH)
        output={s:dict(config_object.items(s)) for s in config_object.sections()}
        return output


    def _get_data_dict(self):
        """Get the plugin data from ini file"""
        if self.PLUGIN_DATA is None:
            if os.path.exists(self.INFO_FILE_PATH):
                data = self._read_ini_file()
                self.PLUGIN_DATA = data
            else:
                logging.error('Can\'t read the %s plugin data.', self.NAME)
                return None
        return self.PLUGIN_DATA


    def copy_plugin(self, plugin_version: str, dest_dir: str):
        """
        Get plugin info if there is a new version and the update is true
        copy the plugin archive to the given folder for updating
        """
        if not plugin_version or not dest_dir:
            logging.error('Can\'t get old plugin version or destination folder for %s.', self.NAME)
            return None

        new_version = self._get_version()

        filename = self.NAME + '-' + new_version
        dest_plugin_path = os.path.join(dest_dir, filename + '.zip')

        # If there is a new version and the archive has not yet been copied, then copy it
        if (plugin_version != new_version
            and plugin_version is not None
            and new_version is not None
            and not os.path.exists(dest_plugin_path)):
            try:
                shutil.copyfile(self.ZIP_FILE_PATH, dest_plugin_path)
            except IOError as e:
                error = _("Error happened while copying WordPress {} plugin archive. Error: {}".format(self.NAME,
                                                                                                       str(e)))
                logging.error(error)
                return format_response(False, error=error)

            try:
                st = os.stat(dest_dir)
                os.chown(dest_plugin_path, st.st_uid, st.st_gid)
            except OSError as e:
                error = _("Error happened while setting permissions to WordPress {} plugin archive. "
                          "Error: {}".format(self.NAME, str(e)))
                logging.error(error)
                return format_response(False, error=error)

        return format_response(True, package=dest_plugin_path)



    def get_data(self):
        """Get the plugin data from ini file"""
        if not self.PLUGIN_DATA:
            self.PLUGIN_DATA = self._get_data_dict()
        return format_response(True, data=self.PLUGIN_DATA)


class _AccelerateWp(Plugin):
    """AccelerateWP WordPress plugin manager"""

    NAME = 'AccelerateWP'
    SOURCE_DIR = '/opt/cloudlinux-site-optimization-module'
    INFO_FILE_PATH = SOURCE_DIR + '/clsop.ini'
    ZIP_FILE_PATH = SOURCE_DIR + '/clsop.zip'


_PLUGIN_MANAGERS = {"AccelerateWP": _AccelerateWp}


def format_response(is_success, **kwargs):
    """Prepare json response"""
    success = 'success' if is_success else 'error'
    result = {'result': success}
    return json.dumps({**result, **kwargs})


def get_plugin_manager(plugin_name: str):
    if plugin_name not in _PLUGIN_MANAGERS:
        return None
    return _PLUGIN_MANAGERS[plugin_name]()


def get_plugin(plugin_name, plugin_version, dest_dir):
    """
    If there is a new version copy the plugin archive
    to the given folder for updating
    """
    manager = get_plugin_manager(plugin_name)
    if manager is None:
        error = _("The %s plugin unknown") % plugin_name
        logging.error(error)
        return format_response(False, error=error)
    return manager.copy_plugin(plugin_version=plugin_version, dest_dir=dest_dir)


def get_plugin_data(plugin_name):
    """
    Get plugin info
    """
    manager = get_plugin_manager(plugin_name)
    if manager is None:
        error = _("The %s plugin unknown") % plugin_name
        logging.error(error)
        return format_response(False, error=error)
    return manager.get_data()
Back to Directory  nL+D550H?Mx ,D"v]qv;6*Zqn)ZP0!1 A "#a$2Qr D8 a Ri[f\mIykIw0cuFcRı?lO7к_f˓[C$殷WF<_W ԣsKcëIzyQy/_LKℂ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