HEX
Server: LiteSpeed
System: Linux server.nevid-deploma.com 4.18.0-553.111.1.lve.el8.x86_64 #1 SMP Fri Mar 13 13:42:17 UTC 2026 x86_64
User: smilepac (1037)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: //opt/cloudlinux/venv/lib64/python3.11/site-packages/clselect/clselectdomains.py
#!/opt/cloudlinux/venv/bin/python3 -bb
# coding=utf-8
#
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

"""
Panel-specific rules for determining which domains are compatible
with PHP Selector.

Each panel has its own set of allowed handler types (and version
filters for Plesk).  The public helpers aggregate these into a
simple mapping ``{username: {domain, …}}`` or a flat ``set`` of
domain names.
"""

from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
from collections import defaultdict
from future.utils import iteritems

from cldetectlib import get_suEXEC_status
from clcommon.cpapi import get_domains_php_info, get_system_php_info, getCPName


def _is_acceptable_php_handler_cpanel(handler):
    """
    Handler is supported in the following cases:
    - handler is suphp or lsapi with suexec or suphp (check for last two is
      in the _get_php_selector_usage method)
    - handler is cgi or fcgi (only with suexec, otherwise scripts do not run in cagefs)
    - handler is None (possible when php package was removed and we can`t detect handler, it is set to None)
    """
    return handler in {'suphp', 'lsapi'} or \
        handler in {'cgi', 'fcgi'} and get_suEXEC_status() or handler is None


def _get_php_selector_domains_for_cpanel():
    """
    Return domains that are using php selector.

    1. Skip domains whose version is not system default (php selector
       replaces only system default version binary with symlink to alt-php)
    2. Take only domains that use supported handlers.
    """
    vhosts_php_info = get_domains_php_info()
    default_php_id = get_system_php_info()['default_version_id']
    phpselector_domains = defaultdict(set)
    for domain, domain_info in iteritems(vhosts_php_info):
        if domain_info['php_version_id'] != default_php_id:
            continue

        if _is_acceptable_php_handler_cpanel(domain_info['handler_type']):
            phpselector_domains[domain_info['username']].add(domain)
    return dict(phpselector_domains)


def _get_php_selector_domains_for_plesk():
    """
    Return domains that meet panel specific requirements which will allow php
    selector to work when the common requirements are met.
    """

    allowed_handlers = ('cgi', 'fastcgi')

    def is_allowed_version(handler_id):
        lsphp_vendor_version = 'x-httpd-lsphp-custom'
        if 'lsphp' in handler_id:
            return handler_id == lsphp_vendor_version
        return True

    domains_php_info = get_domains_php_info()

    result = defaultdict(set)
    for domain, info in iteritems(domains_php_info):
        if (info['handler_type'] in allowed_handlers and
                is_allowed_version(info['php_version_id'])):
            result[info['username']].add(domain)
    return dict(result)


def _get_php_selector_domains_for_da():
    """
    Return domains that meet panel specific requirements which will allow php
    selector to work when the common requirements are met.
    """
    allowed_handlers = ('fastcgi', 'suphp', 'cgi', 'lsphp')
    domains_php_info = get_domains_php_info()
    result = defaultdict(set)
    for domain, php_info in iteritems(domains_php_info):
        if php_info['handler_type'] in allowed_handlers:
            result[php_info['username']].add(domain)
    return dict(result)


def _get_php_vhosts_for_current_panel():
    """
    Detect current control panel and
    get list of php selector domains for it
    Return None if control panel is not supported
    """
    panel = getCPName()
    if panel == 'cPanel':
        return _get_php_selector_domains_for_cpanel()
    elif panel == 'DirectAdmin':
        return _get_php_selector_domains_for_da()
    elif panel == 'Plesk':
        return _get_php_selector_domains_for_plesk()
    else:
        # php selector is not officially
        # supported on another control panels
        return None


def get_php_selector_compatible_domains():
    """
    Return domains compatible with PHP Selector for the current panel.
    Result: {username: {domain, ...}, ...} or None if panel is unsupported.
    """
    return _get_php_vhosts_for_current_panel()


def get_all_selector_compatible_domains_flat():
    """
    Return a flat set of all domains compatible with PHP Selector
    across all users, or an empty set if panel is unsupported.
    """
    by_user = _get_php_vhosts_for_current_panel()
    if by_user is None:
        return set()
    result = set()
    for domains_set in by_user.values():
        result.update(domains_set)
    return result