Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/__pycache__/utils.cpython-311.pyc
�

�m_e����l�ddlmZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlmZmZddlmZddlmZddlmZddlmZmZmZmZddlZddlm Z dd	l!m"Z"m#Z#dd
l$m%Z%ddl&m&Z&m'Z'm(Z(ddl)m*Z*m+Z+m,Z,m-Z-m.Z.ddl/Z/dd
l0m1Z1m2Z2ddl3m4Z4ddl5m6Z6m7Z7ddl8m9Z9m:Z:m;Z;m<Z<ddl=m>Z>m?Z?ddl@mAZAmBZBddlCmDZDmEZEddlFmGZGddlHmIZImJZJmKZKmLZLddlMmNZNmOZOmPZPddlQmZmRZRddlSmTZTmUZUmVZVmWZWmXZXmYZYddlZm[Z[ddl\m]Z]m^Z^ddl_m`Z`ddlHmaZaddlbmcZcddldmeZemfZfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtdd lumvZvmwZwdd!lxmyZydd"lzm{Z{da|d#�Z}Gd$�d%e
j~��Zd�d'e�d(e�fd)�Z�d�d'e�d*e�d+e�fd,�Z�d�d'e�d.e�fd/�Z�d0e�fd1�Z�d2e�fd3�Z�d�d0e�fd4�Z�d5�Z�d6�Z�d�d7e�d0e�fd8�Z�d0e�fd9�Z�dd:�d7e�d0e�fd;�Z�dd:�d7e�fd<�Z�Gd=�d>��Z�d?e�d0e,e�fd@�Z�d0e*e�fdA�Z�dBe�d0dfdC�Z�dDe�d0dfdE�Z�dDe�d0dfdF�Z�dG�Z�dHe�d0e+e�e�ffdI�Z�dJe�dKe,e�fdL�Z�GdM�dNe���Z�e d�dPe�dKe,e�fdQ���Z�dR�Z�dS�Z�e#d�T��dU���Z�d0e*e�fdV�Z�dWe�d0e+e�e�ffdX�Z�d�d0e�fdZ�Z�d[�Z�d\�Z�eGd]�d^����Z�d0e�fd_�Z�d`�Z�da�Z�d0e�fdb�Z�e d0e.e�fdc���Z�dd�Z�de�Z�Gdf�dg��Z�d�dh�Z�Gdi�dj��Z�e dke�fdl���Z�dke�d0e�fdm�Z�d0e�fdn�Z�d0e�fdo�Z�d0e�fdp�Z�d0e�fdq�Z�dre�dse�d0dfdt�Z�dDe�dre�dse�d0dfdu�Z�d0e�fdv�Z�d0e�fdw�Z�d0e+e�e�e�ffdx�Z�dye�dze�d{e�d0e+e�e�e�ffd|�Z�d{e�d0e+e�e�e�ffd}�Z�d~�Z�d0e�fd�Z�e{d0e�fd����Z�d�e�d0e*fd��Z�d��Z�d�e�d�e�d�e�fd��Z�d��Z�dS)��)�absolute_importN)�	dataclass�asdict)�iglob)�Enum)�gettext)�	urlencode�urlparse�	parse_qsl�
urlunparse)�contextmanager)�wraps�	lru_cache)�Path)�socket�AF_UNIX�SOCK_STREAM)�List�Tuple�Optional�Set�ContextManager)�write_file_via_tempfile�
disable_quota)�NoDomain)�ClPwd�drop_privileges)�is_cl_solo_edition�is_cl_shared_pro_edition�is_cl_admin_edition�CLEditionDetectionError)�read_jwt�
decode_jwt)�CLN_JWT_TOKEN_PATH�DEFAULT_JWT_ES_TOKEN_PATH)�
PyJWTError�
exceptions)�
check_license)�docroot�	getCPName�get_customer_login�
get_server_ip)�exec_utility�run_command�demote)r�	wp_config)�	WposError�WPOSLicenseMissing�WpCliUnsupportedException�WpNotExists�WpConfigWriteFailed�PhpBrokenException)�UIConfig)�	in_cagefs�_is_cagefs_enabled)�Feature)�is_panel_feature_supported�)�
setup_logging)�
USER_WPOS_DIR�WPOS_DAEMON_SOCKET_FILE�CLCONFIG_UTILITY�RedisRequiredConstants�CAGEFS_ENTER_USER_BIN�CAGEFS_ENTER_UTIL�CLWPOS_OPT_DIR�ALT_PHP_PREFIX�
EA_PHP_PREFIX�PLESK_PHP_PREFIX�USER_CLWPOS_CONFIG�PUBLIC_OPTIONS�SUPPORTED_PANELS�SUITES_MARKERS�XRAY_MANAGER_UTILITY�XRAY_USER_SOCKET)�pack_data_for_socket�'read_unpack_response_from_socket_client)�RollbackException)�cached_in_scopec����fd�}|S)z'
    Decorator for catching errors
    c���t�tt��a	�|g|�Ri|��S#t$r3}t	|j|j|j|jd���Yd}~dSd}~wt$r�}t|t��rt�|��nt�
|��|j|j|jd�}|jr
|j|d<t	|j|��Yd}~dSd}~wt$rJ}t�
|��t	|jit!|��d���Yd}~dSd}~wwxYw)N)�context�result�issues)rTrU�warning�details)rTrU)�loggerr=�__name__rP�error_and_exit�_is_jsonrT�message�errorsr1�
isinstancer2rW�	exceptionrX�	Exception�str)�self�args�kwargs�e�response�funcs     ��C/opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/utils.py�func_wrapperz!catch_error.<locals>.func_wrapperns�����>�"�8�,�,�F�	M��4��.�t�.�.�.�v�.�.�.�� �	�	�	��4�=��9��)��(�+�+�
�
�
�
�
�
�
�
�
�����
�	4�	4�	4��!�/�0�0�
$����q�!�!�!�!�� � ��#�#�#�#$�9��	�a�i�X�X�H��y�
0�&'�i���#��4�=�(�3�3�3�3�3�3�3�3�3������	M�	M�	M����Q�����4�=�b�C��F�F�*K�*K�L�L�L�L�L�L�L�L�L�����	M���s-�*�
E�(A"�"
E�/BC:�:
E�?E�E�)rhrjs` ri�catch_errorrlis)���
M�M�M�M�M�2��c��eZdZdZd�ZdS)�ExtendedJSONEncoderzz
    Makes it easier to use ENUMs and DATACLASSes in program,
    automatically converting them when json is printed.
    c�>�t|t��r|jSt|tjtjf��r|���St
j|��rt
j|��Stj
�||��S�N)r_r�value�datetime�date�	isoformat�dataclasses�is_dataclassr�json�JSONEncoder�default)rc�objs  rirzzExtendedJSONEncoder.default�s����c�4� � �	��9���c�H�M�8�+<�=�>�>�	#��=�=�?�?�"��#�C�(�(�	+��%�c�*�*�*���'�'��c�2�2�2rmN)rZ�
__module__�__qualname__�__doc__rzrkrmriroro�s-��������3�3�3�3�3rmroF�is_json�	is_prettyc���|rY|r,ttj|ddt�����dSttj|dt�����dSt|��dS)z�
    Print specified dictionary
    :param data_dict: data dictionary to print
    :param is_json: True - print in JSON, False - in text
    :param is_pretty: True - pretty json print, False - none (default)
    :return: None
    �T)�indent�	sort_keys�cls)r�r�N)�printrx�dumpsro)�	data_dictrr�s   ri�_print_dictionaryr��sy�����	R��$�*�Y�q�D�FY�Z�Z�Z�[�[�[�[�[��$�*�Y�$�<O�P�P�P�Q�Q�Q�Q�Q�	�i�����rmr]�
error_codec��d|���vr*|�d��s|�d��|r;|�dt	j��i��t||d���nu	t
t|d��|�di��z��n:#t$r-}t
dt|���d|�d	���Yd
}~nd
}~wwxYwtj
|��d
S)z�
    Print error and exit
    :param is_json:
    :param message: Dictionary with keys "result" as string and optional "context" as dict
    :param error_code: Utility return code on error
    rW�	timestampT�r�rUrTzError: z [�]N)�keys�get�pop�update�timer�r�rb�KeyError�sys�exit)rr]r�rfs    rir[r[�s���G�L�L�N�N�"�"�7�;�;�y�+A�+A�"����I�����8�����T�Y�[�[�1�2�2�2��'�7�d�;�;�;�;�;�	8��#�g�h�'�(�(�7�;�;�y�"�+E�+E�E�F�F�F�F���	8�	8�	8��E�c�!�f�f�f�f�g�g�g�6�7�7�7�7�7�7�7�7�����	8�����H�Z�����s�?9B9�9
C0�#C+�+C0�success�datac��t|t��r)|�|tj��d���t	||d���dS)zo
    Output data wrapper
    :param is_json:
    :param data: data for output to stdout
    :param result:
    )rUr�Tr�N)r_�dictr�r�r�)rr�rUs   ri�
print_datar��sR���$����B����v�D�I�K�K�@�@�A�A�A��d�G�t�4�4�4�4�4�4rm�returnc�0�tj��dkS)zQ
    Detects is we running under root
    :return: True - user, False - root
    r)�os�geteuidrkrmri�is_run_under_userr��s��
�:�<�<�1��rm�safelyc�H�	t��S#t$r|rYdS�wxYw)z�
    Detecting of shared_pro edition depends on jwt token
    There are some cases when we do not fail if there are
    cases with decoding (e.g summary collection)
    F)rr!�r�s ri�is_shared_pro_safelyr��s@���'�)�)�)��"�����	��5�5��	���s�
�!�!c��ttj��}td���st	|��std���o|S)u�
    Сheck if system environment is supported by WPOS
    :return:
        True - CPanel/Plesk on Solo/ CL Shared Pro/ CL Admin
        False - else
    T)�skip_jwt_check)r;r:�WPOSrr�r )r��is_panel_supporteds  ri�is_wpos_supportedr��sR��4�G�L�A�A���d�3�3�3�
8�7K�F�7S�7S�
8�"�$�7�7�7�"�!�"rmc���tj�t|��t��}tj�|��stj|d���dSdS)z@
    Creates {homedir}/.clwpos directory if it's not exists
    i�)�modeN)r��path�join�home_dirr>�isdir�mkdir)�username�
clwpos_dirs  ri�create_clwpos_dir_if_not_existsr��s\������h�x�0�0�-�@�@�J�
�7�=�=��$�$�)�
���%�(�(�(�(�(�(�)�)rmc���t|��d}|�|��std|�d|�d|�����|t|��d��d��S)Nrzdocroot z for domain z should start with �/)r)�
startswithr1�len�lstrip)�domain�homedir�drs   ri�get_relative_docrootr��sq��	�����	�B�
�=�=��!�!�Y��W�2�W�W�6�W�W�g�W�W�X�X�X�
�c�'�l�l�m�m��#�#�C�(�(�(rmr�c�0�t|���}|jS�N�r�)�get_pw�pw_dir)r��pws  rir�r�s��	��	"�	"�	"�B�
�9�rmc�(�t��jSrq)r��pw_namerkrmri�	user_namer�s���8�8��rmr�c�,�t|���jSr�)r��pw_uidr�s ri�user_uidr�
s���8�$�$�$�+�+rmc�x�|rtj|��Stjtj����Srq)�pwd�getpwnam�getpwuidr�r�r�s rir�r�s0���*��|�H�%�%�%��|�B�J�L�L�)�)�)rmc�4�eZdZdZd	dededdfd�Zd�Zd�ZdS)
�WposUserzQ
    Helper class to construct paths to user's WPOS dir and files inside it.
    Nr�r�r�c��||_|�t|��n||_tj�|jt
��|_tj�|jt��|_tj�|jd��|_	tj�|jd��|_
tj�|jd��|_dS)Nz
redis.conf�
redis.sockz.php_info-{file_id})�namer�r�r�r�r>�wpos_dirrH�wpos_config�
redis_conf�redis_socket�php_info)rcr�r�s   ri�__init__zWposUser.__init__s�����	�.5�o���*�*�*�7��
�����T�]�M�B�B��
��7�<�<��
�7I�J�J����'�,�,�t�}�l�C�C����G�L�L����E�E�������T�]�4I�J�J��
�
�
rmc�"�|j|jkSrq�r�)rc�others  ri�__eq__zWposUser.__eq__#s���y�E�J�&�&rmc�*�t|j��Srq)�hashr��rcs ri�__hash__zWposUser.__hash__&s���D�I���rmrq)rZr|r}r~rbr�r�r�rkrmrir�r�sr��������K�K��K�s�K�d�K�K�K�K�'�'�'�����rmr��cmd_dictc	�^�t|��}ttt��5}	|�t
��|�|��|�d��t|��}|�t|t��s,ttd��t|��i����|ddkr8ttd��|d|�d������|cddd��S#t$r%ttd	��d
t
i���t t"t$t&t(jt,f$r0}ttd��t|������d}~wwxYw#1swxYwYdS)
z�
    Send command to CLWPOS daemon via socket
    :param cmd_dict: Command dictionary
    :return: Daemon response as dictionary, None - daemon data/socket error
    �xNzPUnexpected response from daemon. Report this issue to your system administrator.�r]rXrTrUr�z3Daemon was unable to execute the requested command.rTzbCloudLinux AccelerateWP daemon socket (%(filename)s) not found. Contact your system administrator.�filenamez&Unexpected daemon communication error.)rX)rNrrr�connectr?�sendall�
settimeoutrOr_r�r1rrbr��FileNotFoundError�ConnectionError�OSError�IOError�AttributeError�struct�errorr�)r��
bytes_to_send�s�
response_dictrfs     ri�daemon_communicater�*s(��)��2�2�M�	���	%�	%�_��	_�
�I�I�-�.�.�.�
�I�I�m�$�$�$�
�L�L�����C�A�F�F�M��$�J�}�d�,K�,K�$��#�%V�W�W��
�.�.��	 � � � �
�X�&�)�3�3���0e�(f�(f�(5�h�(?�(5�(9�(9�)�(D�(D�F�F�F�F�!�#_�_�_�_�_�_�_�_��$!�	C�	C�	C��G�%I�J�J�'�)@�A�C�C�
C� ��'�>�6�<�QY�Z�	_�	_�	_��G�$L�M�M�WZ�[\�W]�W]�^�^�^�^�����	_����-_�_�_�_����_�_�_�_�_�_s0�F"�CD�AF�/+F�F�F"�"F&�)F&c�R�d}t��jrd}tj�t��td��}tj��}tj
}tj}tj}tj
}tj}tj}dd|j�d|j�d�dtjj�d|�d�d|j�d|j�d�d|j�d|j�d�d|j�d|j�|�d�d|j�d|j�d	�d|j�d|�d	�d
g	S)ak
    Construct list of lines (configuration settings)
    that should be in Wordpress config file to enable redis.
    Please note that deleting of the plugin would flush all keys related to the plugin (site) from redis.
    REDIS_PREFIX and SELECTIVE_FLUSH in wp-config.php would guarantee that plugin will not flush keys unrelated
    to this plugin (site)
    �false�truer�z)// Start of CloudLinux generated section
�define('z', 'z');
z', z);
z'// End of CloudLinux generated section
)�get_server_wide_options�disable_object_cache_bannersr�r�r�r�r>�uuid�uuid4rA�WP_REDIS_PREFIX�WP_REDIS_SCHEME�WP_REDIS_CLIENT�WP_REDIS_SELECTIVE_FLUSH�WP_REDIS_GRACEFUL�WP_REDIS_DISABLE_BANNERSr��val�
WP_REDIS_PATH)	�disable_banners_value�socket_path�prefix_uuid�redis_prefix�redis_schema�redis_client�redis_flush�redis_graceful�disable_bannerss	         ri�redis_cache_config_sectionrKsN��$��� � �=�'� &���'�,�,�x�z�z�=�,�G�G�K��*�,�,�K�)�9�L�)�9�L�)�9�L�(�A�K�+�=�N�,�E�O�8�E�|�(�E�E�l�.>�E�E�E�X�-�;�@�X�X�k�X�X�X�E�|�(�E�E�l�.>�E�E�E�I�~�*�I�I��0B�I�I�I�R�|�(�R�R�l�.>�R��R�R�R�A�{�'�A�A�K�O�A�A�A�K��+�K�K�0E�K�K�K�6�8�8rm�wp_config_pathc��tj�|��}tj�|��st	|���tj�|��s t
td��d|i����dS)z�
    Check that wp-config.php exists inside Wordpress directory.
    :param wp_config_path: absolute path to Wordpress config file
    :raises: WposError
    z)Wordpress config file %(file)s is missing�file�r]rTN)r�r��dirname�existsr4�isfiler1r)r
�wp_paths  ri�check_wp_config_existancerks����g�o�o�n�-�-�G�
�7�>�>�'�"�"�#��'�"�"�"�
�7�>�>�.�)�)�:���(S� T� T�!'�� 8�:�:�:�	:�:�:rm�abs_wp_pathc���ttj|����}t|��t	���dtdt
f�fd�}	tj|��}tt||����}td�
|��|d��dS#ttf$r}t||���d}~wwxYw)z�
    Clear cloudlinux section with redis object cach config from docroot's wp-config.php
    :param abs_wp_path: Absolute path to WordPress
    :raises: WposError
    �liner�c���|�vod|vS)zn
        Filter function that should delete CL config options from the `redis_cache_config_section()`
        r�rk)r�lines_to_filters �ri�__config_filterz1clear_redis_cache_config.<locals>.__config_filter�s����?�*�L�/@��/L�Lrm��N)rbr0r�rr�bool�read�list�filterrr�r�r�r5)rr
r�wp_config_lines�cleared_wp_configrfrs      @ri�clear_redis_cache_configr$zs��������4�4�5�5�N��n�-�-�-�0�2�2�O�M�c�M�d�M�M�M�M�M�M�5�#�.��5�5�� ����!I�!I�J�J������(9� :� :�N�E�R�R�R�R�R���W��5�5�5�!�.�!�4�4�4�����5���s�AB,�,C�=C
�
Cc����ttj|����}t|��	|�d�}tj�|��st
j||��d�tD��}tj	|��}g}|D]\��fd�|�
��D��}|sdSt�fd�tD����s|�����]|dgt���|dd��}td�|��|d	��dS#t t"f$r}t%||���d}~wwxYw)
a
    Create config for redis-cache.
    We use manual copy cause we want to preserve file metadata
    and permissions and also we could add some custom config editing in the future.
    :param abs_wp_path: absolute path to WordPress
    :raises: WposError
    z.backupc�(�i|]}|j|j��Srk)r�r)�.0�constants  ri�
<dictcomp>z-create_redis_cache_config.<locals>.<dictcomp>�s��_�_�_�H�H�M�8�<�_�_�_rmc�,��i|]\}}d|�d��v�
||��S)r��'rk)r'�k�vrs   �rir)z-create_redis_cache_config.<locals>.<dictcomp>�s3���i�i�i���A�_�XY�_�_�_�dh�Mh�Mh��1�Mh�Mh�MhrmNc3�2�K�|]}d|j�d��vV��dS)r�r+Nr�)r'�redis_constantrs  �ri�	<genexpr>z,create_redis_cache_config.<locals>.<genexpr>�s7�����n�n�^�8�.�"5�8�8�8�D�@�n�n�n�n�n�nrmrr<rr)rbr0r�rr�r�shutil�copyrAr�items�any�appendrrr�r�r�r5)	rr
�backup_wp_config�absent_constantsr"�
cleaned_lines�updated_configrfrs	        @ri�create_redis_cache_configr:�s��������4�4�5�5�N��n�-�-�-�5�,�5�5�5���w�~�~�.�/�/�	:��K��(8�9�9�9�_�_�H^�_�_�_��#�.��5�5���
�#�	+�	+�D�i�i�i�i�1A�1G�1G�1I�1I�i�i�i��#�
�����n�n�n�n�Wm�n�n�n�n�n�
+��$�$�T�*�*�*��
�!��
�
'�
)�
)�
��1�2�2�
�
��
	 ����� 7� 7���O�O�O�O�O���W��5�5�5�!�.�!�4�4�4�����5���s�BD9�<A;D9�9E�
E�Ec�<��t����fd���}|S)z,Decorator to check for license validity
    c�L��t��st����|i|��S)zLicense check wrapper)r(r2)rdrerhs  �ri�wrapperz(check_license_decorator.<locals>.wrapper�s2������	'�$�&�&�&��t�T�$�V�$�$�$rm)r)rhr=s` ri�check_license_decoratorr>�s5����4�[�[�%�%�%�%��[�%��Nrmr�c��	t|��\}}||fS#t$r!ttd��d|i����wxYw)z�
    Validates domain, determines it's owner and docroot or exit with error
    :param domain: Domain name to check
    :return: Tuple (username, docroot)
    zNo such domain: %(domain)s.r�r)r)rr1r)r��
document_root�owners   ri�check_domainrB�se��d�&�v����
�u��m�#�#���d�d�d���(E� F� F�QY�[a�Pb�c�c�c�c�d���s	��+Ar��attemptsc��tj}|�|tjz}	t|d��}t	|pd��D]O}	tj|���|��n@#t$rtj	d��Y�LwxYwttd�����n*#t$rttd�����wxYw|S)zE
    Try to take lock on file with specified number of attempts.
    Nza+r<g333333�?zwAnother utility instance is already running. Try again later or contact system administrator in case if issue persists.z%IO error happened while getting lock.)
�fcntl�LOCK_EX�LOCK_NB�open�range�flock�filenor�r��sleep�LockFailedExceptionrr�)r�rC�	lock_type�lock_fd�_s     ri�	lock_filerQ�s���
�I����U�]�"�	�
T��t�T�"�"���x�}�1�%�%�		M�		M�A�
 ���G�N�N�,�,�i�8�8�8�����
 �
 �
 ��
�3������
 ����&�g�/K�'L�'L�M�M�
M�	���T�T�T�!�'�*Q�"R�"R�S�S�S�T�����Ns/�#B-�'A,�*B-�,B
�
B-�B
�
B-�-'Cc��eZdZdZdS)rMz,
    Exception when failed to take lock
    N)rZr|r}r~rkrmrirMrM�s��������	�DrmrM�
�
resource_pathc#�VK�t|dz|��}dV�t|��dS)z�
    Lock a file, than do something.
    Make specified number of attempts to acquire the lock,
    if attempts is None, wait until the lock is released.
    Usage:
    with acquire_lock(path, attempts=1):
       ... do something with files ...
    z.lockN)rQ�release_lock)rTrCrOs   ri�acquire_lockrW�s9�����
��/��:�:�G�	�E�E�E�������rmc��	tj|���tj��n#t$rYnwxYw|���dS)z
    Releases lock file
    N)rErJrK�LOCK_UNr��close)�
descriptors rirVrVsd��
�
��J�%�%�'�'���7�7�7�7���
�
�
���
�����������s�14�
A�Ac���t|j��}t|dgi���}|jr|jst||j���|j}||�d��vrdSdS)z!
    Checks <module> enabled
    z-m)�env�
TF)rb�bin�run_in_cagefs_if_needed�stderr�stdoutr6�split)�php_version�moduler�rU�outs     ri�is_conflict_modules_installedrgst���{����D�
$�d�D�\�r�
:�
:�
:�F�
�}�6�V�]�6� ��v�}�5�5�5�
�-�C�
����4��� � ��t��5rm)�maxsizec��d}d|jkr0t||��r"ttd��d|i����dSdS)zX
    Ensures wp-cli is compatible, e.g some
    php modules may prevent stable work
    �
snuffleupaguszea-php74z�Seems like ea-php74 %(module)s module is enabled. It may cause instabilities while managing Object Caching. Disable it and try againrerN)�
identifierrgr3r)rd�dangerous_modules  ri�wp_cli_compatibility_checkrm#ss��'���[�+�+�+�-�k�;K�L�L�,�'��9c�1d�1d�2:�;K�0L�N�N�N�	N�,�+�+�+rmc��ddg}|S)z@
    Return list of supported handlers according to edition
    zphp-fpm�lsapirk)�	supporteds ri�supported_php_handlersrq2s���G�$�I��rm�hidec�v�ddtjddd|iii��dg}tt|��\}}||fS)zf
    Call cloudlinux-config utility
    to hide/show WPOS icon in user's control panel interface.
    �setz--data�options�
uiSettings�hideAccelerateWPAppz--json)rxr�r-r@)rr�params�
returncoderbs    ri�set_wpos_icon_visibilityrz:sS��	���
�I��/D�d�.K�L�M�N�N��	�F�&�&6��?�?��J���v��rmrwc�F�t���|d��S)zP
    Check the current state of WPOS icon in user's control panel interface
    rv)r7�	get_param��	icon_names ri�is_ui_icon_hiddenrHs���:�:���	�<�8�8�8rmc�h�ttj�t��|g��S)z[
    1. xray utility exists = alt-php-xray package installed
    2. feature is visible
    )�allr�r�rrL)�feature_visibles ri�should_xray_user_agent_enabledr�Ns/��
�����3�4�4��!�"�"�"rmc���t|tj�t��tj�t
��t
d���g��S)z�
    1. Not Solo: it is active by default
    2. xray utility exists = alt-php-xray installed
    3. xray socket exists
    4. end-user plugin was not enabled by admin = hidden in UI
    �hideXrayAppr})r�r�r�rrLrMr)�is_solos ri�should_xray_user_agent_disabledr�VsU���G���G�N�N�#7�8�8��G�N�N�#3�4�4�%�
�>�>�>�@�A�A�Armc��eZdZUdZeed<eed<eed<eed<eed<dZeeed<dZ	eeed	<dZ
eeed
<dZeeed<dZ
eeed<dd�Zed���Zed���ZdS)�ServerWideOptionsz�
    Options holder representing server-wide option
    available for reading for any user on server.

    Only can be changed by root.
    �	show_icon�allowed_suites�visible_suites�supported_suites�hidden_featuresNr��"disable_smart_advice_notifications�disable_smart_advice_reminders�upgrade_url�upgrade_url_cdn�object_cachec	���	�ddlm�	m�i�	fd��	jD����fd��jD���}||vrdSd}|�	jvr|j�dS|j}|�jvr|j�dS|j}|�dSt
t|����}tt|d����}|�
t|��|t��dd||d	���t|��|d<t|��S)
z�
        Append some needed arguments to upgrade url to make it specific for user.
        Please pay attention that we add *customer_name* instead of system user,
        that may be different on plesk.
        r)�PremiumSuite�CDNSuiteProc� ��i|]
}|�j��Srkr�)r'�featurer�s  �rir)z>ServerWideOptions.get_upgrade_url_for_user.<locals>.<dictcomp>�s���W�W�W�g�w��)�W�W�Wrmc� ��i|]
}|�j��Srkr�)r'r�r�s  �rir)z>ServerWideOptions.get_upgrade_url_for_user.<locals>.<dictcomp>�s���U�U�U�W�w��(�U�U�UrmNr��cloudlinux_advantage�provisioning)r�r��	server_ip�m�action�suite)�clwpos.feature_suitesr�r��primary_featuresr�r�r r
r�rr�r+r,r	r)
rcr�r�r��feature_to_suite�
target_url�	url_parts�queryr�r�s
        @@ri�get_upgrade_url_for_userz*ServerWideOptions.get_upgrade_url_for_userxsb����	D�C�C�C�C�C�C�C�

�W�W�W�W��9V�W�W�W�
�U�U�U�U��8T�U�U�U�
��
�*�*�*��4��
��l�3�3�3���'��t��)�J��k�2�2�2��#�+��t��-�J����4���*�-�-�.�.�	��Y�y��|�,�,�-�-��
���*�8�4�4��&���'�$�%�g�.�

�
�	�	�	�!��'�'�	�!���)�$�$�$rmc��ddlm}t��}|jD]"}|�||j���#|S�Nr<��
ALL_SUITES)�feature_suitesr�rtr�r��feature_set)rcr��_allowed_featuresr�s    ri�allowed_featuresz"ServerWideOptions.allowed_features�sY��	/�.�.�.�.�.��E�E���(�	D�	D�E��$�$�Z��%6�%B�C�C�C�C� � rmc��ddlm}t��}|jD]"}|�||j���#|Sr�)r�r�rtr�r�r�)rcr��_visible_featuresr�s    ri�visible_featuresz"ServerWideOptions.visible_features�sW��.�.�.�.�.�.��E�E���(�	D�	D�E��$�$�Z��%6�%B�C�C�C�C� � rm)r�)rZr|r}r~r�__annotations__rr�rr�r�r�rbr�r��propertyr�r�rkrmrir�r�ds
����������O�O�O�����������������37� �(�4�.�7�7�7�9=�&����=�=�=�59�"�H�T�N�9�9�9�!%�K��#��%�%�%�%)�O�X�c�]�)�)�)�.%�.%�.%�.%�`�!�!��X�!��!�!��X�!�!�!rmr�c��ddlm}m}m}m}m}t
���dd��}g}g}|j|j|j|jfD]W}tj
�t|��s�-|�
|��|�
|���Xt|||t|��g���S)zw
    Return default content of /opt/clwpos/public_config.json.
    This file is accessible by all users on server.
    r<)�AWPSuiter��CDNSuiter��SUPPORTED_SUITESrwrv)r�r�r�r�r�)r�r�r�r�r�r�r7r|r�r�r�rrKr5r�r )	r�r�r�r�r��is_icon_hiddenr�r�r�s	         ri�get_default_server_wide_optionsr��s���`�_�_�_�_�_�_�_�_�_�_�_�_�_��Z�Z�)�)�*?��N�N�N��N��N�
�#�X�]�H�M�;�CS�T�%�%���w�~�~�n�U�3�4�4�	�����e�$�$�$����e�$�$�$�$��$�$�%�%��.�/�/�����rmc�L�ddlm}m}m}m}td���x}}tj�t��r:t��}|�d|��}|�d|��}tj
||||gd|||g��}d�|D��S)zY
    Get list of supported suites taking
    into account license and status on CLN.
    r<)r�r�r�r�Tr��is_awp_premium_allowed�is_awp_cdn_allowedc��g|]	}|j��
Srkr�)r'r�s  ri�
<listcomp>z(get_supported_suites.<locals>.<listcomp>�s��+�+�+�5�E�J�+�+�+rm)r�r�r�r�r�r�r�r�rr$�_get_jwt_payloadr��	itertools�compress)r�r�r�r�r�r��jwt�suitess        ri�get_supported_suitesr��s���
������������3G�d�2S�2S�2S�S��/�	�w�~�~�(�)�)�O�� � ��!$���)A�CY�!Z�!Z�� �W�W�%9�;M�N�N��
�
�	�<��;�7�	
�%�'9�;M�N���F�
,�+�F�+�+�+�+rmc��tt��}	t|d���}n,#t$r}t	dt�d|�����d}~wwxYw|S)z1
    Read jwt, verify it and return payload.
    F)�
verify_expz)Unable to detect edition from jwt token: z-. Please, make sure it is not broken, error: N)r"r$r#r&r!)�tokenr�rfs   rir�r��s���
�'�(�(�E�Y���5�1�1�1�����Y�Y�Y�%�'X�Rd�'X�'X�TU�'X�'X�Y�Y�	Y�����Y�����Js�(�
A�A�Ac���ddlm}t��}tj�t��s|Sttd��5}|���}	tj
|��}dD]�}||vr�|�|��}t||��}ttt|tt|���t|������z������}t#|||����|jjd	i|��	t)��}	|jdd�D] }
|
|	vr|j�|
���!n#t.$r
d|_YnwxYw|cddd��S#tjj$r8}t5t7d��t9|��dti����d}~wwxYw#1swxYwYdS)
zL
    Gets server wide options which apply
    as defaults for all users
    r<r��r)r�r�r�NzZFile is corrupted: Please, delete file %(config_file)s or fix the line provided in details�config_filer�rk)r�r�r�r�r�rrIrHrrx�loadsr��getattrr �sortedrt�intersection�setattr�__dict__r�r�r��remove�PermissionError�decoder�JSONDecodeErrorr1rPrb)r��default_options�f�content�
configuration�option_to_merge�suites_from_config�suites_from_defaults�
merged_values�server_suites_allowedr��errs            rir�r�s���
+�*�*�*�*�*�5�7�7�O�
�7�>�>�.�)�)����	
�n�c�	"�	"�(9�a��&�&�(�(��%	9�"&�*�W�"5�"5�M�$\�	
I�	
I��"�-�7�7��%2�%6�%6��%G�%G�"�'.���'P�'P�$�!%�V�C�0D�t�C�&�M(�M(�(4��S��_�_�(E�(E�HG�HG�1G�-H�-H�&I�&I�!J�!J�
����-�H�H�H�H�
,�O�$�+�<�<�m�<�<�<�	
G�(<�(>�(>�%�-�=�a�a�a�@�G�G�E��$9�9�9�'�8�?�?��F�F�F��G��#�
8�
8�
8�48��0�0�0�
8����#�E(9�(9�(9�(9�(9�(9�(9�(9��F�|�+�	9�	9�	9���A�B�B��C���&��7�	9�9�9�
9�����	9����G(9�(9�(9�(9����(9�(9�(9�(9�(9�(9sU�G&�&CF�3E2�1F�2F�F�F�F�G#�+3G�G#�#G&�&G*�-G*c#�&K�t��}|V�tt��5ttd��5}t	jt
|��|��ddd��n#1swxYwYddd��dS#1swxYwYdS)z1Set icon visibility in clwpos public options file�wN)r�rWrIrHrx�dumpr)�public_config_datar�s  ri�write_public_optionsr�5s����1�2�2��
����	�n�	%�	%�1�1����%�%�1�)*��	�&�+�,�,�a�0�0�0�1�1�1�1�1�1�1�1�1�1�1����1�1�1�1�1�1�1�1�1�1�1�1�1�1�1�1����1�1�1�1�1�1s4�B�#A.�"B�.A2	�2B�5A2	�6B�B
�
B
c
�6�t��sttj��sFt	j|fddt
tj��tj	����d�|��Stj��dkr!ttd|�d�������t|t��rtdz|z}ntdg|z}t	j|ft
tj��tj	����ddd	�|��S)
z�
    Wrapper for subprocess to enter cagefs
    do not enter cagefs if:
     - CloudLinux Solo
     - if process already started as user in cagefs
    T)�text�capture_output�
preexec_fnrzInternal error: command zf must not be run as root. Please contact support if you have questions: https://cloudlinux.zendesk.com�r]z --no-io-and-memory-limit �--no-io-and-memory-limit)r�r�r�)r8r;r:�CAGEFS�
subprocess�runr/r�r��getegidr1rr_rbrC)�commandre�with_cagefs_enters   rir`r`AsG���{�{�(�4�W�^�D�D�(��~�g�(�#'�-1�)/��
���b�j�l�l�)K�)K�(�(�!'�	(�(�	(��:�<�<�1����G�-M�w�-M�-M�-M�%N�%N�O�O�O�
O��g�s�#�#�	Z� 1�4P� P�SZ� Z���!2�4N� O�RY� Y���~�/�(�)/��
���b�j�l�l�)K�)K�#'�-1�(�(�!'�	(�(�	(rmc�r�	t���|��S#tj$rYdSwxYw)z
    Returns uid for user
    N)r�get_uid�NoSuchUserExceptionr�s ri�uid_by_namer�^sB����w�w���t�$�$�$���$�����t�t����s� #�6�6c��eZdZdZd�Zed���Zd�Zdede	efd�Z
dd	�Zdd�Zdd�Z
ded
eefd�Zdede	ed
efd�Zdd�Zdede	ed
efd�Zdedeed
efd�Zdedeed
efd�ZdS)�PhpIniConfigz>
    Helper class to update extensions in php .ini files.
    c���||_tjd��|_tjd��|_td|jj��d����f|_dS)Nz-^;\s*extension\s*=\s*(?P<module_name>\w+)\.soz,^\s*extension\s*=\s*(?P<module_name>\w+)\.soz/var/cagefs/*/*/etc/cl.php.d/c�8�|�d��dS)Nr�r�)rc)r�s ri�<lambda>z'PhpIniConfig.__init__.<locals>.<lambda>ts��4�:�:�c�?�?�1�#5�rm)r��user)rd�re�compile�disabled_pattern�enabled_patternr�rk�wildcard_ini_user_locations)rcrds  rir�zPhpIniConfig.__init__lsk��&��� "�
�+[� \� \���!�z�*Y�Z�Z���
�S�d�6F�6Q�S�S�5�5�
7�
7�
7�,
��(�(�(rmc���	|�d��d�d��dS#t$r }tjd|��Yd}~dSd}~wwxYw)z-
        Parse .so extensions safely
        �=r<z.sorz)Cannot parse extension name from line: %sN)rcra�loggingrW)rrfs  ri�_parse_extension_namez"PhpIniConfig._parse_extension_namewsn��
	��:�:�c�?�?�1�%�+�+�E�2�2�1�5�5���	�	�	��O�G��N�N�N��4�4�4�4�4�����	���s�36�
A �A�A c�z�tj�|jj|��}tj�|��sgSt
|��5}|���}ddd��n#1swxYwYg}|D]/}|�|��}|s�|�	|���0|Srq)
r�r�r�rd�dirrrH�	readlinesrr5)rc�ini_path�	full_pathr��ini_content�modules�ext�raw_module_names        ri�get_ini_contentzPhpIniConfig.get_ini_content�s����G�L�L��!1�!5�x�@�@�	��w�~�~�i�(�(�	��I�
�)�_�_�	(���+�+�-�-�K�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(�	(����	(�	(�	(�	(����	,�	,�C�"�8�8��=�=�O�"�
���N�N�?�+�+�+�+��s�A<�<B�Br�r	c��tj�|jj|��}tj�|��s|�||d���dS|�||��dS)NF�r)r�r�r�rdrr�_write_modules�enable_modules)rcr�r	rs    ri�create_custom_inizPhpIniConfig.create_custom_ini�st���G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	/����	�7�5��A�A�A�A�A�
����g�.�.�.�.�.rmNc�"�|rttj�|jj|��}tj�|��r+t
jd|����tj|��|�	dgd���dS)NzCustom ini to be removed: zacceleratewp.iniT)r�)
r�r�r�rdrrr�debug�unlink�update_user_ini)rcr��all_inirs    ri�remove_custom_inizPhpIniConfig.remove_custom_ini�s����	%�����T�%5�%9�4�@�@�I��w�~�~�i�(�(�
%��
�F�9�F�F�G�G�G��	�)�$�$�$����/��D��A�A�A�A�ArmFc	���|jD]Z}t|d��}|D]@}	|�|||||���#t$rt	jd��Y�=wxYw�[dS)Nr�z&Error updating single acceleratewp.ini)r�r�_update_single_inirarr`)rc�ini_filenamer	r��location�cagefs_paths�dir_paths       rirzPhpIniConfig.update_user_ini�s����8�	�	�H� ��&�!1�2�2�L�(�
�
����+�+�H�h���W]�^�^�^�^�� �����%�&N�O�O�O��H�����
�	�	s�>�A�Ac	��|d|��}tj�||��}t|��5t	��5|rItj�|��r)t
jd|��tj|��n5|�	||tj�|�����ddd��n#1swxYwYddd��dS#1swxYwYdS)Nr�z#Custom user ini: %s will be removedr)
r�r�r�rrrrrrr)rcrrr	rr�r�r�s        rirzPhpIniConfig._update_single_ini�s���#�8�F�#�H�-�-���w�|�|�H�l�3�3��
�X�
&�
&�	P�	P����	P�	P��
P��7�>�>�$�'�'�$��M�"G��N�N�N��I�d�O�O�O���#�#�D�'�"�'�.�.��:N�:N�#�O�O�O�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P����	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P�	P����	P�	P�	P�	P�	P�	Ps7�C5�BC�C5�C!	�!C5�$C!	�%C5�5C9�<C9r�c�t��t|d��5}�fd�|D��cddd��S#1swxYwYdS)zU
        Return enabled modules.
        :param path: full path to .ini file
        r�c���h|]I}�j�|����j�|���d����JS)N�module_name�r��match�group)r'rrcs  �ri�	<setcomp>z0PhpIniConfig._enabled_modules.<locals>.<setcomp>�s[���E�E�E�d��+�1�1�$�7�7�C��(�.�.�t�4�4�:�:�=�I�I�C�C�CrmN)rH)rcr�r�s`  ri�_enabled_moduleszPhpIniConfig._enabled_modules�s����
�$��_�_�	E��E�E�E�E�VW�E�E�E�	E�	E�	E�	E�	E�	E�	E�	E�	E�	E�	E�	E����	E�	E�	E�	E�	E�	Es�-�1�1c��tj�|jj|��}tj�|��sdSt
|��|�|��z
}|r|�||��dS)z�
        Enable specified modules in .ini php file.
        :param path: path to .ini file related to php directory
        :param modules: list of modules that should be enabled
        FT)	r�r�r�rdrrrtr&r)rcr�r	r�modules_to_enables     rirzPhpIniConfig.enable_modules�s}���G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	��5���L�L�4�+@�+@��+K�+K�K���	>����	�+<�=�=�=��trmTc����g}|ro��|��}t���|z
�t|��5}��fd�|���D��}ddd��n#1swxYwYt	���D]*}|�d�|�����+|rTtjd|����tjd|����td�
|��|d��dSdS)Nc�<��g|]}��|�����Srk)�_enable_module)r'rr(rcs  ��rir�z/PhpIniConfig._write_modules.<locals>.<listcomp>�s:���!<�!<�!<�%)�"&�!4�!4�T�;L�!M�!M�!<�!<�!<rmzextension={}.so
zPath to write: zContent to write: r�)r&rtrHrr�r5�formatrrrr�)rcrr(r�
new_ini_lines�already_enabledr�res` `     rirzPhpIniConfig._write_modules�sx�����
��	<�"�3�3�I�>�>�O� #�$5� 6� 6�� H���i���
<�A�!<�!<�!<�!<�!<�-.�[�[�]�]�!<�!<�!<�
�
<�
<�
<�
<�
<�
<�
<�
<�
<�
<�
<����
<�
<�
<�
<��.�/�/�	E�	E�F�� � �!4�!;�!;�F�!C�!C�D�D�D�D��	N��M�7�I�7�7�8�8�8��M�>�}�>�>�?�?�?�#�B�G�G�M�$:�$:�I�u�M�M�M�M�M�	N�	Ns�"A+�+A/�2A/c�����tj��jj|��}tj�|��sdSt
|����|��z��rlt|��5}��fd�|�	��D��}ddd��n#1swxYwYtd�|��|d��dS)z�
        Disable specified modules in .ini php file.
        :param path: path to .ini file related to php directory
        :param modules: list of modules that should be disabled
        Fc�<��g|]}��|�����Srk)�_disable_module)r'r�modules_to_disablercs  ��rir�z0PhpIniConfig.disable_modules.<locals>.<listcomp>�s:���!<�!<�!<�%)�"&�!5�!5�d�<N�!O�!O�!<�!<�!<rmNrr,T)r�r�r�rdrrrtr&rHrr)rcr�r	rr�r.r3s`     @ri�disable_moduleszPhpIniConfig.disable_modules�s�����G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	��5� ��\�\�D�,A�,A�)�,L�,L�L���	N��i���
<�A�!<�!<�!<�!<�!<�-.�[�[�]�]�!<�!<�!<�
�
<�
<�
<�
<�
<�
<�
<�
<�
<�
<�
<����
<�
<�
<�
<�
$�B�G�G�M�$:�$:�I�u�M�M�M��ts�"B2�2B6�9B6rr(c���|j�|��}|�U|�d��}||vr<|�|��|�d�����S|S)zV
        Search for disabled module in line, uncomment line to enable module.
        Nr!�;)r�r#r$r�r�)rcrr(r#r!s     rir+zPhpIniConfig._enable_module�st���%�+�+�D�1�1�����+�+�m�4�4�K��/�/�/�!�(�(��5�5�5��{�{�3�'�'�.�.�0�0�0��rmr3c�z�|j�|��}|�|�d��}||vrd|��S|S)zT
        Search for enabled module in line, comment line to disable module.
        Nr!r6r")rcrr3r#r!s     rir2zPhpIniConfig._disable_modulesN���$�*�*�4�0�0�����+�+�m�4�4�K��0�0�0�!�4�z�z�!��rmrq�F)T)rZr|r}r~r��staticmethodrrrbrrrrrrr&rrrr4r+r2rkrmrir�r�gs���������	
�	
�	
�����\�����$/�c�/�D��I�/�/�/�/�B�B�B�B�����P�P�P�P�E�S�E�S��X�E�E�E�E��3���c���t�����N�N�N�N�$�C��$�s�)�������"
�3�
�3�s�8�
��
�
�
�
�	�C�	�S��X�	�#�	�	�	�	�	�	rmr�c�8�ttj��sdSt��sT|�t	td������td|gtjdd�ztjdd�z}nCt��s3tt�����rtdgtjz}ndStj|tjtji���}|���tj|j��dS)	a%
    All user-related actions must run inside of cagefs for security reasons.
    If solo just return because cagefs is only for shared and shared pro
    If root executed, we enter into user cagefs if user is pointed
    If not in cagefs and cagefs is enabeled for user enter into cagefs
    Nz�Internal Error: root enters into CageFS without specifying usernamePlease contact support if you have questions: https://cloudlinux.zendesk.comr�r�r<�)r�)rb�stdinr])r;r:r�r�r1rrBr��argvr8r9r�rCr��Popenrbr<�communicater�ry)r��cmd�ps   ri�_run_clwpos_as_user_in_cagefsrBs��&�g�n�5�5���������<��G�1�%�%����
�%�)����x����|�$�&)�h�q�r�r�l�3����[�[��/�Y�[�[�A�A�A�� �"<�=���H���	�����S�Z�s�y�b�I�I�I�A��M�M�O�O�O��H�Q�\�����rmc�Z�eZdZdZdeddfd�Zd
d�Zd
d�Zdefd�Z	e
defd	���ZdS)�RedisConfigurePidFilezm
    Helper class that provides methods to work with
    pid files of php redis configuration processes.
    �
php_prefixr�Nc�X�|�d�|_tt|j��|_dS)Nz-cloudlinux.pid)�_pid_file_namerrDr�)rcrEs  rir�zRedisConfigurePidFile.__init__6s+��!+�<�<�<������)<�=�=��	�	�	rmc���|j�d��5}|�tt	j������ddd��dS#1swxYwYdS)Nr�)r�rH�writerbr��getpid�rcr�s  ri�createzRedisConfigurePidFile.create:s���
�Y�^�^�C�
 �
 �	&�A�
�G�G�C��	���$�$�%�%�%�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&����	&�	&�	&�	&�	&�	&s�4A�A �#A c�n�|j���r|j���dSdSrq)r��is_filerr�s rir�zRedisConfigurePidFile.remove>s<���9�����	��I��������	�	rmc�4�|j���Srq)r�rNr�s rirzRedisConfigurePidFile.existsBs���y� � �"�"�"rmc�6�|���sdS|j���5}	t|��������cddd��S#t$rYnwxYw	ddd��n#1swxYwYdS)N���)rr�rH�intr�strip�
ValueErrorrKs  ri�pidzRedisConfigurePidFile.pidEs����{�{�}�}�	��2�
�Y�^�^�
�
�	��
��1�6�6�8�8�>�>�+�+�,�,�	�	�	�	�	�	�	�	���
�
�
���
�����		�	�	�	�	�	�	�	�	�	�	����	�	�	�	�
�rs.�B�2A1�1
A>�;B�=A>�>B�B�B)r�N)
rZr|r}r~rbr�rLr�rrr�rRrUrkrmrirDrD0s���������
>�3�>�4�>�>�>�>�&�&�&�&�����#��#�#�#�#���S�����X���rmrDrEc#�K�t|��}	|���dV�|���dS#|���wxYw)za
    Context manager for creating pid file of current process.
    Removes pid file on exit.
    N)rDrLr�)rE�pid_files  ri�create_pid_filerXQs_����%�Z�0�0�H��������
����������������������s�A�Ac��t|��}tj��dkr|���S	t	j|j��}d|���vS#ttj	f$rYdSwxYw)z�
    Find out if PHP redis configuration process is running.
    Based on looking for presence of pid files.
    For root also checks process existence.
    r�enable_redisF)
rDr�r�r�psutil�ProcessrUr�rT�
NoSuchProcess)rErW�processs   ri�"is_php_redis_configuration_runningr__s���%�Z�0�0�H�	�z�|�|�q������ � � ���.���.�.��������/�/����,�-�����u�u����s�.A+�+B�Bc�*�tt��S)zE
    Find out if alt-PHP redis configuration process is running.
    )r_rErkrmri�&is_alt_php_redis_configuration_runningraos��.�n�=�=�=rmc�*�tt��S�zD
    Find out if ea-PHP redis configuration process is running.
    )r_rFrkrmri�%is_ea_php_redis_configuration_runningrdvs��.�m�<�<�<rmc�*�tt��Src)r_rGrkrmri�(is_plesk_php_redis_configuration_runningrf}s��.�.>�?�?�?rmc�V�t��pt��p
t��S)za
    Find out if redis configuration process
    is running for any PHP (ea-php or alt-php).
    )rardrfrkrmri�is_redis_configuration_runningrh�s.��
2�3�3�6�0�2�2�6�3�5�5�6rm�new_user�old_userc����t�j��5}|���}ddd��n#1swxYwY��fd�|D��}td�|���jd��dS)z;
    Replace user's wpos directory path in redis.conf.
    Nc�P��g|]"}|��j�j����#Srk)�replacer��r'rrirjs  ��rir�z%update_redis_conf.<locals>.<listcomp>�s:������?C����X�&��(9�:�:���rmrr)rHr�rrr�)rirjr��redis_conf_lines�
updated_liness``   ri�update_redis_confrq�s�����
�h�!�	"�	"�)�a��;�;�=�=��)�)�)�)�)�)�)�)�)�)�)����)�)�)�)������GW����M��B�G�G�M�2�2�H�4G��O�O�O�O�Os�8�<�<c�2���	tj|��}n9#t$r,}td|�d|��tj���Yd}~dSd}~wwxYw��fd�|D��}t
d�|��tj|��d��dS)z<
    Replace user's redis socket path in wp-config.php.
    z=Error occurred during opening wp-config.php located in path "z": )rNc�f��g|]-}�j|vr |��j�j��n|��.Srk)r�rmrns  ��rir�z$update_wp_config.<locals>.<listcomp>�sU������
�� �D�(�(�	
���X�*�H�,A�B�B�B�.2���rmrr)	r0rr�r�r�rarr�r�)rrirjr"rfrps ``   ri�update_wp_configrt�s������#�.��5�5��������
�6�"-�6�6�23�6�6�<?�J�	H�	H�	H�	H���������������
�����$����M�
�B�G�G�M�2�2�I�N�;�4O�4O�QV�W�W�W�W�Ws��
A�!A
�
Ac�t�tjtj����}|���S)z!
    Get parent process PID.
    )r[r\r�rJ�ppid)�procs ri�get_parent_pidrx�s'���>�"�)�+�+�&�&�D��9�9�;�;�rmc�D�tgd�d���\}}}|dkrdSdS)z`
    Detect CL WPOS daemon presence in system
    :return: True - daemon works / False - No
    )�
/sbin/service�clwpos_monitoring�statusT��return_full_outputrF)r.)ryrPs  ri�_is_monitoring_daemon_existsr�s8��#�#S�#S�#S�hl�m�m�m��J��1��Q����u��4rmc��tjd|��tgd�d���\}}}|stddgd���\}}}|||fS)z5
    Update systemd unit file and reload systemd
    z//usr/share/cloudlinux/clwpos_monitoring.service)�/usr/bin/systemctl�enablezclwpos_monitoring.serviceTr}r�z
daemon-reload)r1r2r.)�systemd_unit_file�retcoderbras    ri�$_update_clwpos_daemon_config_systemdr��sx���K�A�CT�U�U�U�)�*g�*g�*g�=A�C�C�C��G�V�V��p�"-�/C�_�.U�jn�"o�"o�"o������F�F�"�"rmr�r��is_module_allowed_on_serverc���d\}}}dtj��vrtgd�d���\}}}n|s|rt|��\}}}|stgd�d���\}}}|||fS)z4
    Install WPOS daemon to system and start it
    )rNN�el6)z/sbin/chkconfigz--addr{Tr})rzr{�start)�platform�releaser.r�)r�r�r�r�rbras      ri�_install_daemon_internalr��s���
,��G�V�V��� �"�"�"�"�"-�._�._�._�AE�#G�#G�#G�������	^�6�	^�'K�K\�&]�&]�#�G�V�V��G�"-�.]�.]�.]�AE�#G�#G�#G������F�F�"�"rmc���d}t��}|stj�|��rt	|��t��rdSt
|||��S)a�
    Install WPOS daemon to server if need:
        - if daemon already present - do nothing;
        - on CL Solo install daemon always;
        - on CL Shared Pro install daemon if module allowed
    On solo and if /etc/systemd/system/clwpos_monitoring.service present it will be updated always
    We do not need restart installed daemon here, it's done in rpm_posttrans.sh
    :param is_module_allowed_on_server: True/False
    z-/etc/systemd/system/clwpos_monitoring.service)rrr)rr�r�rr�rr�)r�r�r�s   ri�install_monitoring_daemonr��sl��H�� �"�"�G��@�"�'�.�.�!2�3�3�@�,�->�?�?�?�#�%�%���y�#�G�->�@[�\�\�\rmc��dd|�d�i}	t|��}n#t$rYdSwxYw|�d��S)Nr�zget-z-statusFr|)r�r1r�)�service�command_get_service_status_dict�
daemon_results   ri�get_status_from_daemonr��sd��'0�2I��2I�2I�2I�&J�#��*�+J�K�K�
�
�������u�u��������X�&�&�&s��
(�(c� �td��S)N�redis�r�rkrmri�redis_is_runningr�s��!�'�*�*�*rmc� �td��S)N�	litespeedr�rkrmri�litespeed_is_runningr�s��!�+�.�.�.rm�	attributec��ddlm}|t����}tj�|d��}	t
|��5}tj|��|cddd��S#1swxYwYdS#tttjf$r!}tj
d|��gcYd}~Sd}~wwxYw)z7
    Return attribute's value from info.json file.
    r)�get_admin_config_directoryz	info.jsonNz,Error during reading of "info.json" file: %s)r�r�r�r�r�r�rHrx�loadr�r�r�rr`)r�r��admin_config_dir�	info_jsonr�rfs      ri�_get_data_from_info_jsonr�s��A�@�@�@�@�@�1�1�(�*�*�=�=������-�{�;�;�I��
�)�_�_�	+���9�Q�<�<�	�*�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+����	+�	+�	+�	+�	+�	+���X�t�3�4������J�A�N�N�N��	�	�	�	�	�	��������sA�B�A5�(B�5A9�9B�<A9�=B�B?�B:�4B?�:B?c�d�t}tj��rtj}||��Srq)rr�r��
contextlib�nullcontext)r�rTs  ri�drop_permissions_if_neededr�s/���G�	�z�|�|�)��(���7�8���rmr�r�r�c��ddlm}||�|��vrdnd}	t|j|d���d}n#t
$rd}YnwxYw|rd}|S)Nr)�
WposDaemon�active�no)r�r��pendingF)�
clwpos.daemonr�r�r��!DAEMON_GET_UPGRADE_ATTEMPT_STATUSr1)r�r�r�r��subscription_status�
is_pendings      ri�get_subscription_statusr�'s���(�(�(�(�(�(�&-�1A�1E�1E�e�1L�1L�&L�&L�(�(�RV���'�!�C��)
�)
�����
�
�������
�
�
������(�'���s�A�A�Ac�\�d\}}}	tt��}n4#ttf$r dd�t��dfcYSwxYw	t|��nG#tj$r	d\}}}Yn0tj$r	d\}}}Yntj	$r	d\}}}YnwxYw|||fS)z�
    JWT token check. Mostly copied from cllib, but with some accelerate-wp tunes, including:
    - clsolo, cladmin tokens are now valid
    - no need to check for shared, because our tools just don't work on shared
    )T�OKNFzJWT file {} read errorN)FzJWT token issuer is invalidN)FzJWT token expiredN)FzJWT token format errorN)
r"r%r�r�r-r#r'�InvalidIssuerError�ExpiredSignatureErrorr&)�success_flag�
error_message�token_strings   ri�jwt_token_checkr�=s��1A�-�L�-��W�� 9�:�:�����W��W�W�W��.�5�5�6O�P�P�RV�V�V�V�V�W����Z��<� � � � ���(�_�_�_�4^�1��m�\�\�\��+�U�U�U�4T�1��m�\�\�\�� �Z�Z�Z�4Y�1��m�\�\�\�Z�������4�4s-��.A�
A�A"�"B&�9B&�B&�%B&)FF)r<)r�r8rq)rS)rw)��
__future__rr�rvrsr�rr�r�r1r�r�r�rxr�rEr�r�rr�globr�enumrrrP�urllib.parser	r
rrr[r
�	functoolsrr�pathlibrrrr�typingrrrrrr��secureiorr�clcommon.cpapi.cpapiexceptionsr�clcommon.clpwdrr�clcommon.lib.cleditionrrr r!�clcommon.lib.jwt_tokenr"r#�clcommon.lib.constsr$r%r�r&r'�cllicenselibr(�clcommon.cpapir)r*r+r,�clcommon.utilsr-r.r/�clwposr0�clwpos.cl_wpos_exceptionsr1r2r3r4r5r6�clcommon.ui_configr7�clcommon.clcagefsr8r9�clcommon.constr:r;�logsetupr=�clwpos.constantsr>r?r@rArBrCrDrErFrGrHrIrJrKrLrM�socket_utilsrNrO�user.website_check.errorsrP�clwpos.scoped_cacherQrYrlryrorr�r�rRr[r�r�r�r�r�r�rbr�r�r�r�r�r�rrr$r:r>rBrQrarMrWrVrgrmrqrzrr�r�r�r�r�r�r�r�r`r�r�rBrDrXr_rardrfrhrqrtrxrr�r�r�r�r�r�r�r�r�r�rkrmri�<module>r�s���'�&�&�&�&�&���������������������	�	�	�	�	�	�	�	�
�
�
�
�
�
�
�
�
�
�
�
���������
�
�
�
�������������)�)�)�)�)�)�)�)������������� � � � � � ��������������
�
�
�%�%�%�%�%�%�&�&�&�&�&�&�&�&�������/�/�/�/�/�/�/�/�/�/�=�=�=�=�=�=�=�=�=�=�=�=�=�=�����;�;�;�;�;�;�;�;�3�3�3�3�3�3�1�1�1�1�1�1�1�1�������������8�7�7�7�7�7�7�7�M�M�M�M�M�M�M�M�&�&�&�&�&�&�&�&�&�&�&�&�&�&�P�P�P�P�P�P�P�P�P�P�P�P�<�<�<�<�<�<�<�<�<�<�%�%�%�%�%�%�%�%�����������������(�'�'�'�'�'�;�;�;�;�;�;�;�;�"�"�"�"�"�"�5�5�5�5�5�5�#�#�#�#�#�#�������������������������������������&X�W�W�W�W�W�W�W�8�8�8�8�8�8�/�/�/�/�/�/�	
�����B3�3�3�3�3�$�*�3�3�3���$��4�����&��D��4��S�����*	5�	5��	5�D�	5�	5�	5�	5��4�����������
"�
"�t�
"�
"�
"�
"�)�)�)�)�)�)���s��c�����
�3�����!%�,�,�,�#�,��,�,�,�,�#�*�*�*��*�*�*�*���������*_��_�(�4�.�_�_�_�_�B8�D��I�8�8�8�8�@:�c�:�d�:�:�:�:�5�#�5�$�5�5�5�5�0&5�3�&5�4�&5�&5�&5�&5�R���d��d��s�C�x��d�d�d�d��C��8�C�=�����2	�	�	�	�	�)�	�	�	������x��}�������
�
�
�
�
�
� ��4����N�N���N���S�	������4��E�#�s�(�O�����9�9�$�9�9�9�9�"�"�"�
A�
A�
A��R!�R!�R!�R!�R!�R!�R!���R!�j�):�����<,�,�,�6
�
�
�29�!2�29�29�29�29�j�1�n�->�?�1�1�1���1�(�(�(�:���f�f�f�f�f�f�f�f�R����@��������B�
��
�
�
���
�
�3�
�4�
�
�
�
� >��>�>�>�>�=�t�=�=�=�=�@�$�@�@�@�@�6��6�6�6�6�
P��
P�H�
P��
P�
P�
P�
P�X�#�X��X�X�X�RV�X�X�X�X�&�������d�����	#�u�S�#�s�]�?S�	#�	#�	#�	#�#�d�#�s�#�:>�#�CH��c�SV��CW�#�#�#�#�(]�4�]�E�#�s�TW�-�DX�]�]�]�]�*'�'�'�+�$�+�+�+�+��/�d�/�/�/���/���������"����d��3�������,5�5�5�5�5rm