????

Your IP : 3.137.211.1


Current Path : /opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/__pycache__/
Upload File :
Current File : //opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/__pycache__/utils.cpython-311.pyc

�

tf���	��ddlmZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddl
Z
ddlZddlZddlZddlZddlZddlmZmZddlmZddlmZddlmZddlmZmZmZmZddl m!Z!ddl"Z"dd	lm#Z#dd
l$m%Z%m&Z&ddl'm(Z(ddl)m)Z)m*Z*m+Z+dd
lm,Z,m-Z-m.Z.m/Z/m0Z0ddl1Z1ddl2m3Z3m4Z4ddl5m6Z6ddl7m8Z8m9Z9ddl:m;Z;ddl<m=Z=m>Z>ddl?m@Z@mAZAddlBmCZCmDZDddlEmFZFmGZGddlHmIZIddlJmKZKmLZLmMZMmNZNddlOmPZPmQZQmRZRddlSmZmTZTddlUmVZVmWZWmXZXmYZYmZZZm[Z[ddl\m]Z]ddl^m_Z_m`Z`ddlambZbddlJmcZcdd ldmeZedd!lfmgZgmhZhmiZimjZjmkZkmlZlmmZmmnZnmoZompZpmqZqmrZrmsZsmtZtmuZudd"lvmwZwmxZxdd#lymzZzdd$l{m|Z|ej}rdd%l~mZda�d&�Z�Gd'�d(e
j���Z�d�d*e�d+e�fd,�Z�d�d*e�d-e�d.e�fd/�Z�d�d*e�d1e�fd2�Z�d3e�fd4�Z�d5e�fd6�Z�d3e�fd7�Z�d8ej�fd9�Z�d:�Z�d�d;e�d3e�fd<�Z�d3e�fd=�Z�dd>�d;e�d3e�fd?�Z�dd>�d;e�fd@�Z�GdA�dB��Z�dCe�d3e.e�fdD�Z�d3e,e�fdE�Z�dFe�d3dfdG�Z�dHe�d3dfdI�Z�dHe�d3dfdJ�Z�dK�Z�dLe�d3e-e�e�ffdM�Z�dNe�dOe.e�fdP�Z�GdQ�dRe���Z�e#d�dTe�dOe.e�fdU���Z�dV�Z�e|d�dY���Z�dZe�d3e-e�e�ffd[�Z�d�d3e�fd]�Z�d^�Z�d_�Z�eGd`�da����Z�d3e�fdb�Z�dc�Z�dd�Z�d3e�fde�Z�e#d3e0e�fdf���Z�dg�Z�dh�Z�Gdi�dj��Z�d�dk�Z�Gdl�dm��Z�e#dne�fdo���Z�dne�d3e�fdp�Z�d3e�fdq�Z�d3e�fdr�Z�d3e�fds�Z�d3e�fdt�Z�due�dve�d3dfdw�Z�dHe�due�dve�d3dfdx�Z�d3e�fdy�Z�d3e�fdz�Z�d3e-e�e�e�ffd{�Z�d|e�d}e�d3e-e�e�e�ffd~�Z�d}e�d3e-e�e�e�ffd�Z�d��Z�e|d3e�fd����Z�e|d3e�fd����Z�d�e�d3e,fd��Z�d��Z�d�e�d�e�d�e�fd��Z�d��Z�d��Z�dS)��)�absolute_importN)�	dataclass�asdict)�iglob)�Enum)�gettext)�	urlencode�urlparse�	parse_qsl�
urlunparse)�Version)�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)�mkdir��is_cl_shared_pro_edition�CLEditionDetectionError)�read_jwt�
decode_jwt)�CLN_JWT_TOKEN_PATH�DEFAULT_JWT_ES_TOKEN_PATH)�
PyJWTError�
exceptions)�
check_license)�docroot�get_domain_login�
get_server_ip�cpusers)�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�SUITES_MARKERS�XRAY_MANAGER_UTILITY�XRAY_USER_SOCKET)�pack_data_for_socket�'read_unpack_response_from_socket_client)�RollbackException)�cached_in_scope)�PHPc����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)rUrV�warning�details)rUrV)�loggerr>�__name__rP�error_and_exit�_is_jsonrU�message�errorsr2�
isinstancer3rX�	exceptionrY�	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_wrapperrs�����>�"�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�)rirks` rj�catch_errorrmms)���
M�M�M�M�M�2��c�(��eZdZdZ�fd�Zd�Z�xZS)�ExtendedJSONEncoderzz
    Makes it easier to use ENUMs and DATACLASSes in program,
    automatically converting them when json is printed.
    c�D��d|d<t��jdi|��dS)NF�ensure_asciirl)�super�__init__)rdrf�	__class__s  �rjrtzExtendedJSONEncoder.__init__�s0���!&��~�������"�"�6�"�"�"�"�"rnc��t|t��r|jSt|tjtjf��r|���St|t��rt|��Stj	|��rtj
|��Stj�
||��S�N)r`r�value�datetime�date�	isoformatr
rc�dataclasses�is_dataclassr�json�JSONEncoder�default)rd�objs  rjr�zExtendedJSONEncoder.default�s����c�4� � �	+��9��
��h�m�X�->�?�
@�
@�	+��=�=�?�?�"�
��W�
%�
%�	+��s�8�8�O�
�
%�c�
*�
*�	+��%�c�*�*�*���'�'��c�2�2�2rn)r[�
__module__�__qualname__�__doc__rtr��
__classcell__)rus@rjrprp�sQ���������#�#�#�#�#�	3�	3�	3�	3�	3�	3�	3rnrpF�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)�printr~�dumpsrp)�	data_dictr�r�s   rj�_print_dictionaryr��sy�����	R��$�*�Y�q�D�FY�Z�Z�Z�[�[�[�[�[��$�*�Y�$�<O�P�P�P�Q�Q�Q�Q�Q�	�i�����rnr^�
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
    rX�	timestampT�r�rVrUzError: z [�]N)�keys�get�pop�update�timer�r�rc�KeyError�sys�exit)r�r^r�rgs    rjr\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:
    )rVr�Tr�N)r`�dictr�r�r�)r�r�rVs   rj�
print_datar��sR���$����B����v�D�I�K�K�@�@�A�A�A��d�G�t�4�4�4�4�4�4rn�returnc�0�tj��dkS)zQ
    Detects is we running under root
    :return: True - user, False - root
    r)�os�geteuidrlrnrj�is_run_under_userr��s��
�:�<�<�1��rn�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)
    Fr �r�s rj�is_shared_pro_safelyr��s@���'�)�)�)��"�����	��5�5��	���s�
�!�!c�4�ttj��S)u�
    Сheck if system environment is supported by WPOS
    :return:
        True - CPanel/Plesk on Solo/ CL Shared Pro/ CL Admin
        False - else
    )r<r;�WPOSrlrnrj�is_wpos_supportedr��s��&�g�l�3�3�3rn�user_pwc��tj�|jt��}tj�|��st
|d���dSdS)z@
    Creates {homedir}/.clwpos directory if it's not exists
    i�)�modeN)r��path�join�pw_dirr?�isdirr)r��
clwpos_dirs  rj�create_clwpos_dir_if_not_existsr��sT������g�n�m�<�<�J�
�7�=�=��$�$�&�
�j�u�%�%�%�%�%�%�&�&rnc���t|��d}|�|��std|�d|�d|�����|t|��d��d��S)Nrzdocroot z for domain z should start with �/)r*�
startswithr2�len�lstrip)�domain�homedir�drs   rj�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�(�(�(rn�usernamec�0�t|���}|jS�N�r�)�get_pwr�)r��pws  rj�home_dirr�	s��	��	"�	"�	"�B�
�9�rnc�(�t��jSrw)r��pw_namerlrnrj�	user_namer�s���8�8��rnr�c�,�t|���jSr�)r��pw_uidr�s rj�user_uidr�s���8�$�$�$�+�+rnc�x�|rtj|��Stjtj����Srw)�pwd�getpwnam�getpwuidr�r�r�s rjr�r�s0���*��|�H�%�%�%��|�B�J�L�L�)�)�)rnc�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_dirrI�wpos_config�
redis_conf�redis_socket�php_info)rdr�r�s   rjrtzWposUser.__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��
�
�
rnc�"�|j|jkSrw�r�)rd�others  rj�__eq__zWposUser.__eq__+s���y�E�J�&�&rnc�*�t|j��Srw)�hashr��rds rj�__hash__zWposUser.__hash__.s���D�I���rnrw)r[r�r�r�rcrtr�r�rlrnrjr�r�sr��������K�K��K�s�K�d�K�K�K�K�'�'�'�����rnr��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^rYrUrVr�z3Daemon was unable to execute the requested command.rUzbCloudLinux AccelerateWP daemon socket (%(filename)s) not found. Contact your system administrator.�filenamez&Unexpected daemon communication error.)rY)rNrrr�connectr@�sendall�
settimeoutrOr`r�r2rrcr��FileNotFoundError�ConnectionError�OSError�IOError�AttributeError�struct�errorr�)r��
bytes_to_send�s�
response_dictrgs     rj�daemon_communicater�2s(��)��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�uuid4rB�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	         rj�redis_cache_config_sectionrSsN��$��� � �=�'� &���'�,�,�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�8rn�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^rUN)r�r��dirname�existsr5�isfiler2r)r�wp_paths  rj�check_wp_config_existancerss����g�o�o�n�-�-�G�
�7�>�>�'�"�"�#��'�"�"�"�
�7�>�>�.�)�)�:���(S� T� T�!'�� 8�:�:�:�	:�:�:rn�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�rl)r�lines_to_filters �rj�__config_filterz1clear_redis_cache_config.<locals>.__config_filter�s����?�*�L�/@��/L�Lrn��N)rcr1r�rr�bool�read�list�filterrr�r�r�r6)rrr�wp_config_lines�cleared_wp_configrgrs      @rj�clear_redis_cache_configr(�s��������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��Srl)r�r)�.0�constants  rj�
<dictcomp>z-create_redis_cache_config.<locals>.<dictcomp>�s��_�_�_�H�H�M�8�<�_�_�_rnc�,��i|]\}}d|�d��v�
||��S)r��'rl)r+�k�vrs   �rjr-z-create_redis_cache_config.<locals>.<dictcomp>�s3���i�i�i���A�_�XY�_�_�_�dh�Mh�Mh��1�Mh�Mh�MhrnNc3�2�K�|]}d|j�d��vV��dS)r�r/Nr�)r+�redis_constantrs  �rj�	<genexpr>z,create_redis_cache_config.<locals>.<genexpr>�s7�����n�n�^�8�.�"5�8�8�8�D�@�n�n�n�n�n�nrnrr=r r!)rcr1r�rr�r�shutil�copyrBr#�items�any�appendrrr�r�r�r6)	rr�backup_wp_config�absent_constantsr&�
cleaned_lines�updated_configrgrs	        @rj�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)r3)rerfris  �rj�wrapperz(check_license_decorator.<locals>.wrapper�s2������	'�$�&�&�&��t�T�$�V�$�$�$rn)r)rirAs` rj�check_license_decoratorrB�s5����4�[�[�%�%�%�%��[�%��Nrnr�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*rr2r)r��
document_root�owners   rj�check_domainrF�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�rG�	lock_type�lock_fd�_s     rj�	lock_filerU�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)rQz,
    Exception when failed to take lock
    N)r[r�r�r�rlrnrjrQrQ�s��������	�DrnrQ�
�
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)rU�release_lock)rXrGrSs   rj�acquire_lockr[�s9�����
��/��:�:�G�	�E�E�E�������rnc��	tj|���tj��n#t$rYnwxYw|���dS)z
    Releases lock file
    N)rIrNrO�LOCK_UNr��close)�
descriptors rjrZrZsd��
�
��J�%�%�'�'���7�7�7�7���
�
�
���
�����������s�14�
A�A�php_versionrRc��d}d|jkr5|�|��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 again�modulerN)�
identifier�is_extension_loadedr4r)r`�dangerous_modules  rj�wp_cli_compatibility_checkrgsw��'���[�+�+�+��/�/�0@�A�A�,�'��9c�1d�1d�2:�;K�0L�N�N�N�	N�,�+�+�+rn�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)r~r�r.rA)rh�params�
returncode�stdouts    rj�set_wpos_icon_visibilityrq*sS��	���
�I��/D�d�.K�L�M�N�N��	�F�&�&6��?�?��J���v��rnrmc�F�t���|d��S)zP
    Check the current state of WPOS icon in user's control panel interface
    rl)r8�	get_param��	icon_names rj�is_ui_icon_hiddenrv8s���:�:���	�<�8�8�8rnc�h�ttj�t��|g��S)z[
    1. xray utility exists = alt-php-xray package installed
    2. feature is visible
    )�allr�r�rrL)�feature_visibles rj�should_xray_user_agent_enabledrz>s/��
�����3�4�4��!�"�"�"rnc���ttj�t��tj�t
��t
d���g��S)z�
    1. xray utility exists = alt-php-xray installed
    2. xray socket exists
    3. end-user plugin was not enabled by admin = hidden in UI
    �hideXrayApprt)rxr�r�rrLrMrvrlrnrj�should_xray_user_agent_disabledr}FsP�������3�4�4��G�N�N�#3�4�4�%�
�>�>�>�@�A�A�Arnc��eZdZUdZeed<eed<eed<eed<eed<dZeeed<dZ	eeed	<dZ
eeed
<dZeeed<dZee
ed<dZee
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_wordpress_plugin�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��Srlr�)r+�featurer�s  �rjr-z>ServerWideOptions.get_upgrade_url_for_user.<locals>.<dictcomp>rs���W�W�W�g�w��)�W�W�Wrnc� ��i|]
}|�j��Srlr�)r+r�r�s  �rjr-z>ServerWideOptions.get_upgrade_url_for_user.<locals>.<dictcomp>ss���U�U�U�W�w��(�U�U�UrnNr��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)
rdr�r�r��feature_to_suite�
target_url�	url_parts�queryr�r�s
        @@rj�get_upgrade_url_for_userz*ServerWideOptions.get_upgrade_url_for_userfsd����	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��|�,�,�-�-��
���(��6�:�:��&���'�$�%�g�.�

�
�	�	�	�!��'�'�	�!���)�$�$�$rnc��ddlm}t��}|jD]"}|�||j���#|S�Nr=��
ALL_SUITES)�feature_suitesr�rjr�r��feature_set)rdr��_allowed_featuresr�s    rj�allowed_featuresz"ServerWideOptions.allowed_features�sY��	/�.�.�.�.�.��E�E���(�	D�	D�E��$�$�Z��%6�%B�C�C�C�C� � rnc��ddlm}t��}|jD]"}|�||j���#|Sr�)r�r�rjr�r�r�)rdr��_visible_featuresr�s    rj�visible_featuresz"ServerWideOptions.visible_features�sW��.�.�.�.�.�.��E�E���(�	D�	D�E��$�$�Z��%6�%B�C�C�C�C� � rn)r�)r[r�r�r�r"�__annotations__rr�rr�r�r�r�rcr�r��propertyr�r�rlrnrjrrQs"����������O�O�O�����������������37� �(�4�.�7�7�7�9=�&����=�=�=�<@�)�8�D�>�@�@�@�59�"�H�T�N�9�9�9�!%�K��#��%�%�%�%)�O�X�c�]�)�)�)�.%�.%�.%�.%�`�!�!��X�!��!�!��X�!�!�!rnrc��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_SUITESrmrl)r�r�r�r�r�)r�r�r�r�r�r�r8rsr�r�r�rrKr9rr$)	r�r�r�r�r��is_icon_hiddenr�r�r�s	         rj�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�$�$�$�$��$�$�%�%��.�/�/�����rnc�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��
Srlr�)r+r�s  rj�
<listcomp>z(get_supported_suites.<locals>.<listcomp>�s��+�+�+�5�E�J�+�+�+rn)r�r�r�r�r�r�r�r�rr%�_get_jwt_payloadr��	itertools�compress)r�r�r�r�r�r��jwt�suitess        rj�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�+�+�+�+rnc��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�rgs   rjr�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�rl)r�r�r�r�r�rrJrLr#r~�loadsr��getattrr$�sortedrj�intersection�setattr�__dict__r�r�r��remove�PermissionError�decoder�JSONDecodeErrorr2rTrc)r��default_options�f�content�
configuration�option_to_merge�suites_from_config�suites_from_defaults�
merged_values�server_suites_allowedr��errs            rjr�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�tj�t��st	dt�d����t��}|V�t
t��5ttd��5}tj
t|��|��ddd��n#1swxYwYddd��dS#1swxYwYdS)z1Set icon visibility in clwpos public options filez6Can't write public options as configuration directory z does not exist�wN)r�r�rrEr�r�r[rJrLr~�dumpr)�public_config_datar�s  rj�write_public_optionsr�$sH�����7�>�>�.�)�)�
��d�^�d�d�d�
�
�	
�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�1s6�%C�;#B*�C�*B.	�.C�1B.	�2C�C�	Cc
��t��}d|vr
|r||dd<tjdt|��t|�d������t��st
tj��sFtj
|fddttj
��tj����d�|��Stj
��dkr tt!d��|z����t#|t��rt$d	z|z}nt$d
g|z}tj
|fttj
��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
    �env�LANG�*Executing command: %s with environment: %sT)�text�capture_output�
preexec_fnrz�Internal error: command %s 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�)�get_locale_from_envars�logging�inforcr�r9r<r;�CAGEFS�
subprocess�runr0r�r��getegidr2rr`rD)�commandrf�locale�with_cagefs_enters    rj�run_in_cagefs_if_neededr�6s���$�
%�
%�F�����6�� &��u�
�f���L�=��W����V�Z�Z��&�&�'�'�)�)�)��{�{�(�4�W�^�D�D�(��~�g�(�#'�-1�)/��
���b�j�l�l�)K�)K�(�(�!'�	(�(�	(��:�<�<�1����G�-M�%N�%N�PW�%X�Y�Y�Y�
Y��g�s�#�#�	Z� 1�4P� P�SZ� Z���!2�4N� O�RY� Y���~�/�(�)/��
���b�j�l�l�)K�)K�#'�-1�(�(�!'�	(�(�	(rnc�r�	t���|��S#tj$rYdSwxYw)z
    Returns uid for user
    N)r�get_uid�NoSuchUserExceptionr�s rj�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�Zd�Zdede	ed
efd�Zed���Zdd�Zd�Zdede	ed
efd�Zdedeed
efd�Zdedeed
efd�ZdS)�PhpIniConfigz>
    Helper class to update extensions in php .ini files.
    c���||_tjd��|_tjd��|_tjd��|_t
d|jj��d����f|_dS)Nz-^;\s*extension\s*=\s*(?P<module_name>\w+)\.soz,^\s*extension\s*=\s*(?P<module_name>\w+)\.soz1^\s*;?\s*extension\s*=\s*(?P<module_name>\w+)\.soz/var/cagefs/*/*/etc/cl.php.d/c�8�|�d��dS)Nr�r�)�split)r�s rj�<lambda>z'PhpIniConfig.__init__.<locals>.<lambda>rs��4�:�:�c�?�?�1�#5�rn)r��user)	r`�re�compile�disabled_pattern�enabled_pattern�	extensionr�rd�wildcard_ini_user_locations)rdr`s  rjrtzPhpIniConfig.__init__is}��&��� "�
�+[� \� \���!�z�*Y�Z�Z�����$X�Y�Y���
�S�d�6F�6Q�S�S�5�5�
7�
7�
7�,
��(�(�(rnc���	|�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)r�rbr�rX)rrgs  rj�_parse_extension_namez"PhpIniConfig._parse_extension_nameusn��
	��:�:�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|Srw)
r�r�r�r`�dirrrL�	readlinesrr9)rd�ini_path�	full_pathr��ini_content�modules�ext�raw_module_names        rj�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�r`rr�_write_modules�enable_modules)rdr�r	rs    rj�create_custom_inizPhpIniConfig.create_custom_ini�st���G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	/����	�7�5��A�A�A�A�A�
����g�.�.�.�.�.rnNc�"�|rttj�|jj|��}tj�|��r+t
jd|����tj|��|�	dgd���dS)NzCustom ini to be removed: zacceleratewp.iniT)r�)
r�r�r�r`rrr��debug�unlink�update_user_ini)rdr��all_inirs    rj�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�ArnFc	���|jD]Z}t|d��}|D]@}	|�|||||���#t$rt	jd��Y�=wxYw�[dS)Nr�z&Error updating single acceleratewp.ini)r�r�_update_single_inirbr�ra)rd�ini_filenamer	r��location�cagefs_paths�dir_paths       rjrzPhpIniConfig.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�rrrr�rrr)rdrrr	rr�r�r�s        rjrzPhpIniConfig._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+rrds  �rj�	<setcomp>z0PhpIniConfig._enabled_modules.<locals>.<setcomp>�s[���E�E�E�d��+�1�1�$�7�7�C��(�.�.�t�4�4�:�:�=�I�I�C�C�CrnN�rL�rdr�r�s`  rj�_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�	E��-�1�1c�t��t|d��5}�fd�|D��cddd��S#1swxYwYdS)Nr�c���h|]I}�j�|����j�|���d����JSr!)r�r$r%r&s  �rjr'z0PhpIniConfig._extensions_list.<locals>.<setcomp>�sV���?�?�?���~�+�+�D�1�1�=��N�(�(��.�.�4�4�]�C�C�=�=�=rnr(r)s`  rj�_extensions_listzPhpIniConfig._extensions_list�s����
�$��_�_�	?��?�?�?�?�PQ�?�?�?�	?�	?�	?�	?�	?�	?�	?�	?�	?�	?�	?�	?����	?�	?�	?�	?�	?�	?r+c��tj�|jj|��}tj�|��sdSt
jd|����t|��}|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
        FzEnable such extensions: T)
r�r�r�r`rrr�rrjr)rdr�r	r�modules_to_enables     rjrzPhpIniConfig.enable_modules�s����G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	��5��
�:��:�:�;�;�;���L�L���	>����	�+<�=�=�=��trnc��d|�d�S)z-
        redis -> extension=redis.so
        z
extension=z.so
rl)rcs rj�_format_as_ini_extzPhpIniConfig._format_as_ini_ext�s��
*�F�)�)�)�)rnTc	���	�g}tjd|����|r�t|��}t|��5}|���D]O�	t�	�fd�|D����rtjd�	�d|�d����:|��	���P	ddd��n#1swxYwYt|��}d|vr|�d����|D]C}��	|��}tjd	|����|�|���D|rTtjd
|����tjd|����td�|��|d
��dSdS)Nz,Such extensions are required to be enabled: c3�F�K�|]}��|���vV��dSrw)r2)r+r
rrds  ��rjr4z.PhpIniConfig._write_modules.<locals>.<genexpr>�s6�����]�]�C�4�2�2�3�7�7�4�?�]�]�]�]�]�]rnzSkip z, z will be added further�redisc�,�|�d��S�Nr5)�endswith)�xs rjr�z-PhpIniConfig._write_modules.<locals>.<lambda>�s��a�j�j��.A�.A�rn)�keyzAppending lines to be written: zPath to write: zContent to write: r �)r�rrjrLrr8r9r��sortr2rr�)
rdrr0r�
new_ini_linesr��sorted_modulesrc�extension_liners
`        @rjrzPhpIniConfig._write_modules�s�����
��
�X�EV�X�X�Y�Y�Y��	/� #�$5� 6� 6���i���
/�A��K�K�M�M�/�/�D��]�]�]�]�]�K\�]�]�]�]�]�!��
�&_�d�&_�&_�6G�&_�&_�&_�`�`�`� �!�(�(��.�.�.�.�	/�
/�
/�
/�
/�
/�
/�
/�
/�
/�
/�
/����
/�
/�
/�
/� � 1�2�2���n�$�$����$A�$A��B�B�B�$�	1�	1�F�!�4�4�V�<�<�N��M�L�N�L�L�M�M�M�� � ��0�0�0�0��	N��M�7�I�7�7�8�8�8��M�>�}�>�>�?�?�?�#�B�G�G�M�$:�$:�I�u�M�M�M�M�M�	N�	Ns�A%B.�.B2�5B2c��tj�|jj|��}tj�|��sgSt
|�|����}tj	d|�d|����|S)zH
        Reads <ext>.ini file and loads all required extensions
        zRequired extensions for z are: )
r�r�r�r`rrr$r.r�r)rdr�r�required_moduless    rj�get_required_modulesz!PhpIniConfig.get_required_modules�s~���G�L�L��!1�!5�t�<�<�	��w�~�~�i�(�(�	��I��� 5� 5�i� @� @�A�A���
�O��O�O�=M�O�O�P�P�P��rnc�����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|]}��|�����Srl)�_disable_module)r+r�modules_to_disablerds  ��rjr�z0PhpIniConfig.disable_modules.<locals>.<listcomp>s:���!<�!<�!<�%)�"&�!5�!5�d�<N�!O�!O�!<�!<�!<rnNr r;T)r�r�r�r`rrrjr*rLrr)rdr�r	rr�r=rFs`     @rj�disable_moduleszPhpIniConfig.disable_moduless�����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�9B6rr0c���|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�)rdrr0r$r"s     rj�_enable_modulezPhpIniConfig._enable_modulest���%�+�+�D�1�1�����+�+�m�4�4�K��/�/�/�!�(�(��5�5�5��{�{�3�'�'�.�.�0�0�0��rnrFc�z�|j�|��}|�|�d��}||vrd|��S|S)zT
        Search for enabled module in line, comment line to disable module.
        Nr"rIr#)rdrrFr$r"s     rjrEzPhpIniConfig._disable_module%sN���$�*�*�4�0�0�����+�+�m�4�4�K��0�0�0�!�4�z�z�!��rnrw)F)T)r[r�r�r�rt�staticmethodrrrcrrrrrrr*r.r"rr2rrBrGrJrErlrnrjr�r�ds���������

�

�

�����\�����$/�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�	�#�	�	�	�	�	�	rnr�c��ttj��sdSt��sT|�t	td������td|gtjdd�ztjdd�z}nCt��s3tt�����rtdgtjz}ndSdt��i}tjd	t!|��t!|����t#j|tjtj|�
��}|���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�r�r�)rp�stdinr�)r<r;r�r�r2rrCr��argvr9r:r�rDr�r�r�rcr��PopenrprO�communicater�ro)r��cmdr��ps    rj�_run_clwpos_as_user_in_cagefsrU1sI��&�g�n�5�5���������<��G�1�%�%����
�%�)����x����|�$�&)�h�q�r�r�l�3����[�[��/�Y�[�[�A�A�A�� �"<�=���H���	���)�+�+�
,�C��L�=�s�3�x�x��S���R�R�R����S�Z�s�y� �	"�	"�	"�A��M�M�O�O�O��H�Q�\�����rnc�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_namerrEr�)rdrXs  rjrtzRedisConfigurePidFile.__init__Ys+��!+�<�<�<������)<�=�=��	�	�	rnc���|j�d��5}|�tt	j������ddd��dS#1swxYwYdS)Nr�)r�rL�writercr��getpid�rdr�s  rj�createzRedisConfigurePidFile.create]s���
�Y�^�^�C�
 �
 �	&�A�
�G�G�C��	���$�$�%�%�%�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&�	&����	&�	&�	&�	&�	&�	&s�4A�A �#A c�n�|j���r|j���dSdSrw)r��is_filerr�s rjr�zRedisConfigurePidFile.removeas<���9�����	��I��������	�	rnc�4�|j���Srw)r�rar�s rjrzRedisConfigurePidFile.existses���y� � �"�"�"rnc�6�|���sdS|j���5}	t|��������cddd��S#t$rYnwxYw	ddd��n#1swxYwYdS)N���)rr�rL�intr#�strip�
ValueErrorr^s  rj�pidzRedisConfigurePidFile.pidhs����{�{�}�}�	��2�
�Y�^�^�
�
�	��
��1�6�6�8�8�>�>�+�+�,�,�	�	�	�	�	�	�	�	���
�
�
���
�����		�	�	�	�	�	�	�	�	�	�	����	�	�	�	�
�rs.�B�2A1�1
A>�;B�=A>�>B�B�B)r�N)
r[r�r�r�rcrtr_r�r"rr�rerhrlrnrjrWrWSs���������
>�3�>�4�>�>�>�>�&�&�&�&�����#��#�#�#�#���S�����X���rnrWrXc#�K�t|��}	|���dV�|���dS#|���wxYw)za
    Context manager for creating pid file of current process.
    Removes pid file on exit.
    N)rWr_r�)rX�pid_files  rj�create_pid_filerkts_����%�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)
rWr�r�r�psutil�Processrhr�rg�
NoSuchProcess)rXrj�processs   rj�"is_php_redis_configuration_runningrr�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.
    )rrrFrlrnrj�&is_alt_php_redis_configuration_runningrt�s��.�n�=�=�=rnc�*�tt��S�zD
    Find out if ea-PHP redis configuration process is running.
    )rrrGrlrnrj�%is_ea_php_redis_configuration_runningrw�s��.�m�<�<�<rnc�*�tt��Srv)rrrHrlrnrj�(is_plesk_php_redis_configuration_runningry�s��.�.>�?�?�?rnc�V�t��pt��p
t��S)za
    Find out if redis configuration process
    is running for any PHP (ea-php or alt-php).
    )rtrwryrlrnrj�is_redis_configuration_runningr{�s.��
2�3�3�6�0�2�2�6�3�5�5�6rn�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����#Srl)�replacer��r+rr|r}s  ��rjr�z%update_redis_conf.<locals>.<listcomp>�s:������?C����X�&��(9�:�:���rnr r!)rLr�rrr�)r|r}r��redis_conf_lines�
updated_liness``   rj�update_redis_confr��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|��.Srl)r�r�r�s  ��rjr�z$update_wp_config.<locals>.<listcomp>�sU������
�� �D�(�(�	
���X�*�H�,A�B�B�B�.2���rnr r!)	r1r#r�r�r��stderrrr�r�)rr|r}r&rgr�s ``   rj�update_wp_configr��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.
    )rnror�r]�ppid)�procs rj�get_parent_pidr��s'���>�"�)�+�+�&�&�D��9�9�;�;�rnc�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/)rorTs  rj�_is_monitoring_daemon_existsr��s8��#�#S�#S�#S�hl�m�m�m��J��1��Q����u��4rnc��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)r5r6r/)�systemd_unit_file�retcoderpr�s    rj�$_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�"�"rnr��is_module_allowed_on_serverc���d\}}}dtj��vrtgd�d���\}}}n|rt|��\}}}|stgd�d���\}}}|||fS)z4
    Install WPOS daemon to system and start it
    )rNN�el6)z/sbin/chkconfigz--addr�Tr�)r�r��start)�platform�releaser/r�)r�r�r�rpr�s     rj�_install_daemon_internalr��s���,��G�V�V��� �"�"�"�"�"-�._�._�._�AE�#G�#G�#G������'�	^�'K�K\�&]�&]�#�G�V�V��G�"-�.]�.]�.]�AE�#G�#G�#G������F�F�"�"rnc��d}tj�|��rt|��t	��rdSt||��S)a�
    Install WPOS daemon to server if need:
        - if daemon already present - do nothing;
        - 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)rr r )r�r�rr�r�r�)r�r�s  rj�install_monitoring_daemonr�sW��H��	�w�~�~�'�(�(�@�,�->�?�?�?�#�%�%���y�#�$5�7R�S�S�Srnc��dd|�d�i}	t|��}n#t$rYdSwxYw|�d��S)Nr�zget-z-statusFr�)r�r2r�)�service�command_get_service_status_dict�
daemon_results   rj�get_status_from_daemonr�sd��'0�2I��2I�2I�2I�&J�#��*�+J�K�K�
�
�������u�u��������X�&�&�&s��
(�(c� �td��Sr7�r�rlrnrj�redis_is_runningr�"s��!�'�*�*�*rnc� �td��S)N�	litespeedr�rlrnrj�litespeed_is_runningr�'s��!�+�.�.�.rn�	attributec���ddlm}|t����}tj�|d��}	t
|��5}tj|��|cddd��S#1swxYwYdS#ttj
f$r6}tjd|��ttd�����d}~wt$rttd�����wxYw)z7
    Return attribute's value from info.json file.
    r)�get_admin_config_directoryz	info.jsonNz,Error during reading of "info.json" file: %sz�Failed to retrieve data about php version which is currently used. Daemon is not available and cache data is malformed, please try again and contact your administrator if the issue persists.z�Failed to retrieve data about php version which is currently used. Daemon is not available and cache data is not available. Contact your administrator if the issue persists.)r�r�r�r�r�r�rLr~�loadr�r�r�rar2rTr�)r�r��admin_config_dir�	info_jsonr�rgs      rj�_get_data_from_info_jsonr�,sm��A�@�@�@�@�@�1�1�(�*�*�=�=������-�{�;�;�I�P�
�)�_�_�	+���9�Q�<�<�	�*�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+�	+����	+�	+�	+�	+�	+�	+���d�*�+�Q�Q�Q���J�A�N�N�N���O�P�P�Q�Q�	Q������P�P�P���N�O�O�P�P�	P�P���s;�B�A5�(B�5A9�9B�<A9�=B�C3�1C	�	*C3c�d�t}tj��rtj}||��Srw)rr�r��
contextlib�nullcontext)r�rUs  rj�drop_permissions_if_neededr�Cs/���G�	�z�|�|�)��(���7�8���rnr�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_STATUSr2)r�r�r�r��subscription_status�
is_pendings      rj�get_subscription_statusr�Ns���(�(�(�(�(�(�&-�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��formatr$r(�InvalidIssuerError�ExpiredSignatureErrorr')�success_flag�
error_message�token_strings   rj�jwt_token_checkr�ds��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&c��tj�d��p^tj�d��p?tj�d��p tj�d��pdS)z�
    Locale could be set via those envvars, let`s get them in same priority gettext does
    for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'):
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LC_MESSAGES = "UTF-8",
    LANG = "uk_UA.UTF-8"
    �LANGUAGE�LC_ALL�LC_MESSAGESr��en_US)r��environr�rlrnrjr�r�zsh��
�J�N�N�:�&�&�
��z�~�~�h�'�'�
��z�~�~�m�,�,�
��z�~�~�f�%�%�
��	rn)FF)r=)r�rw)rW)r`rR)rm)��
__future__rr�r|ryr�r�r�r�r5r�r�r�r~r��typingrIr�r�rr�globr�enumrrrT�urllib.parser	r
rr�packaging.versionr
rnr�	functoolsrr�pathlibrrrrrrrrrr��secureiorr�clcommon.cpapi.cpapiexceptionsr�clcommon.clpwdrr�clcommon.clcaptainr�clcommon.lib.cleditionr!r"�clcommon.lib.jwt_tokenr#r$�clcommon.lib.constsr%r&r�r'r(�cllicenselibr)�clcommon.cpapir*r+r,r-�clcommon.utilsr.r/r0�clwposr1�clwpos.cl_wpos_exceptionsr2r3r4r5r6r7�clcommon.ui_configr8�clcommon.clcagefsr9r:�clcommon.constr;r<�logsetupr>�clwpos.constantsr?r@rArBrCrDrErFrGrHrIrJrKrLrM�socket_utilsrNrO�user.website_check.errorsrP�clwpos.scoped_cacherQ�
TYPE_CHECKING�clwpos.php.baserRrZrmrrpr"r�r�rer\r�r�r�r��
struct_passwdr�r�rcr�r�r�r�r�r�rrr(r>rBrFrUrbrQr[rZrgrqrvrzr}rr�r�r�r�r�r�r�r�rUrWrkrrrtrwryr{r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rlrnrj�<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�&�&�&�&�&�&�&�&�&�&�&�&�&�&�L�L�L�L�L�L�L�L�L�L�L�L�<�<�<�<�<�<�<�<�<�<�%�%�%�%�%�%�%�%�����������������(�'�'�'�'�'�;�;�;�;�;�;�;�;�"�"�"�"�"�"�5�5�5�5�5�5�#�#�#�#�#�#�����������������������������������$X�W�W�W�W�W�W�W�8�8�8�8�8�8�/�/�/�/�/�/�	��$�#�#�#�#�#�#�	
�����B3�3�3�3�3�$�*�3�3�3�*��$��4�����&��D��4��S�����*	5�	5��	5�D�	5�	5�	5�	5��4�����������4�4�4�4�4�4�&�S�->�&�&�&�&�)�)�)���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��}�������
�
�
��N�N�N���N��4��E�#�s�(�O�����9�9�$�9�9�9�9�"�"�"�A�A�A��S!�S!�S!�S!�S!�S!�S!���S!�l�):�����<,�,�,�8
�
�
�29�!2�29�29�29�29�j�1�n�->�?�1�1�1���1�""(�"(�"(�J���J�J�J�J�J�J�J�J�Z����D��������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�	#�	#�	#�	#�#��#�RV�#�[`�ad�fi�kn�an�[o�#�#�#�#�&T�4�T�E�#�s�TW�-�DX�T�T�T�T�&'�'�'��+�$�+�+�+���+��/�d�/�/�/���/�P��P��P�P�P�P�.����d��3�������,5�5�5�,
�
�
�
�
rn