PATH:
opt
/
imunify360
/
venv
/
lib
/
python3.11
/
site-packages
/
defence360agent
/
wordpress
/
__pycache__
/
Editing: cli.cpython-311.pyc
� ���i* � � � d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZm Z m Z d dlmZm Z d dlmZmZmZ d dlmZ d dlmZ e j e� � Zd ed efd�Zded efd �Zded efd�Zdefd�Zdefd�Zdefd�Z ded efd�Z!defd�Z"defd�Z#defd�Z$defd�Z%defd�Z& e d�� � defd�� � Z'd� Z(defd�Z)dS )� N)�Path)� StrictVersion)� check_run� CheckRunError�async_lru_cache)�PLUGIN_PATH�PLUGIN_SLUG)�build_command_for_user�get_php_binary_path� wp_wrapper)�log_message)�WPSite�version_str�returnc � � t | t � � sdS | � � � }|sdS t |� � dS # t $ r Y dS w xY w)z1Validate if a string is a valid semantic version.FT)� isinstance�str�stripr � ValueError)r �trimmed_strs �R/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/wordpress/cli.py�_validate_semverr su � � �k�3�'�'� ��u� �#�#�%�%�K�� ��u���k�"�"�"��t��� � � ��u�u����s �A � A�A�outputc �b � | sdS | � � � }|sdS t |� � dk r+|d � � � }t |� � r|S t |� � dk r+|d � � � }t |� � r|S t dd| iddd � � � dS )z� Extract version from WP CLI output, trying both first and last parts. Args: output: The raw output from WP CLI Returns: The extracted version string or None if no valid version found Nr � ���zMFailed to extract valid semver version from WP CLI output. Output: '{output}'r �warning� wordpressz#wp-plugin-version-extraction-failed)�format_args�level� component�fingerprint)�split�lenr r r )r �parts� first_part� last_parts r �_extract_version_from_outputr( * s� � � � ��t� �L�L�N�N�E�� ��t� �5�z�z�A�~�~��1�X�^�^�%�%� ��J�'�'� ��� �5�z�z�A�~�~��"�I�O�O�%�%� ��I�&�&� ��� � ��v�&���9� � � � � �4� �sitec � � K � t | � � � d{V ��}|dz dz dz }|� � � sdS t j d� � } t |� � 5 }|D ][}|� |� � }|rB|� d� � }t |� � r|c cddd� � S ddd� � dS �\ ddd� � n# 1 swxY w Y n4# t $ r'}t � d||� � Y d}~dS d}~ww xY wdS )z� Parse the version of imunify-security plugin by reading the main plugin file. Args: site: WordPress site object containing docroot path Returns: str: Plugin version or None if not found or invalid N�plugins�imunify-securityzimunify-security.phpz\* Version:\s*([0-9.]+)r z=Failed to read plugin file to determine version number %s: %s)�get_content_dir�exists�re�compile�open�search�groupr � Exception�logger�error) r* �content_dir�plugin_file�version_pattern�f�line�match�version�es r �_parse_version_from_plugin_filer@ V s� � � � � (��-�-�-�-�-�-�-�-�K��i��"4�4�7M�M� � ����� ��t��j�!;�<�<�O�� �+� � � $�!�� $� $��'�.�.�t�4�4��� $�#�k�k�!�n�n�G�'��0�0� $�&��� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $� $�$� $� $� $� $� $� $� $� $� $� $� $� $���� $� $� $� $��� � � � ����K�� � � � � �t�t�t�t�t����� ���� �4sU �C �AC � C �-C �.C �;C �=C � C � C �C �C � D�D�Dc � �\ K � t j | j � � j }t | |� � � d{V ��}g t || j � � �d�d�t t � � �d�d�}t ||� � }t � d|� �� � t |� � � d{V �� dS )zFInstall the Imunify Security WordPress plugin on given WordPress site.N�plugin�install� --activate�--forcezInstalling wp plugin � �pwd�getpwuid�uid�pw_namer r �docrootr r r r6 �infor �r* �username�php_path�args�commands r �plugin_installrR s� � � � ��|�D�H�%�%�-�H�(��x�8�8�8�8�8�8�8�8�H�� �H�d�l� +� +���� �� �K��� � �� � �D� %�X�t�4�4�G� �K�K�1��1�1�2�2�2� �G� � ���������r) c � �\ K � t j | j � � j }t | |� � � d{V ��}g t || j � � �d�d�t t � � �d�d�}t ||� � }t � d|� �� � t |� � � d{V �� dS )z� Update the Imunify Security WordPress plugin on given WordPress site. Currently, this is the same as install, but in the future it may differ. NrB rC rD rE zUpdating wp plugin rF rM s r � plugin_updaterT � s� � � � � �|�D�H�%�%�-�H�(��x�8�8�8�8�8�8�8�8�H�� �H�d�l� +� +���� �� �K��� � �� � �D� %�X�t�4�4�G� �K�K�/�g�/�/�0�0�0� �G� � ���������r) c � �> K � t j | j � � j }t | |� � � d{V ��}g t || j � � �d�d�t �d�}t ||� � }t � d|� �� � t |� � � d{V �� dS )z<Uninstall the imunify-security wp plugin from given wp site.NrB � uninstallz--deactivatezUninstalling wp plugin )rG rH rI rJ r r rK r r r6 rL r rM s r �plugin_uninstallrW � s� � � � ��|�D�H�%�%�-�H�(��x�8�8�8�8�8�8�8�8�H�� �H�d�l� +� +���� �� � � ��D� %�X�t�4�4�G� �K�K�3�'�3�3�4�4�4� �G� � ���������r) c � � K � t | � � � d{V �� dS # t $ r'}t � d| |� � Y d}~dS d}~ww xY w)z�Attempt to uninstall the plugin, returning False on failure. Safe wrapper around plugin_uninstall for use in cleanup paths where failure should be logged but not raised. NTz5Failed to uninstall plugin from %s during cleanup: %sF)rW r5 r6 r )r* r7 s r �try_plugin_uninstallrY � s} � � � � ��t�$�$�$�$�$�$�$�$�$��t��� � � ����C��� � � � �u�u�u�u�u����� ���s � � A�A�Ac � �t K � t j | j � � j }t | |� � � d{V ��}g t || j � � �d�d�t �d�}t ||� � }t � d|� �� � t |� � � d{V ��}|� d� � � � � }t |� � S # t $ r+}t � d|j � � Y d}~dS d}~wt$ $ r&}t � d|� � Y d}~dS d}~ww xY w) z� Get the version of the imunify-security wp plugin installed on given WordPress site. Uses WP CLI to get the version. NrB �getz--field=versionzGetting wp plugin version �utf-8z0Failed to get wp plugin version. Return code: %sz-Failed to decode wp plugin version output: %s)rG rH rI rJ r r rK r r r6 rL r �decoder r( r r7 � returncode�UnicodeDecodeError)r* rN rO rP rQ �resultr r? s r �_get_plugin_versionra � s} � � � � �|�D�H�%�%�-�H�(��x�8�8�8�8�8�8�8�8�H�� �H�d�l� +� +���� �� � � ��D� %�X�t�4�4�G� �K�K�6�W�6�6�7�7�7�� ��)�)�)�)�)�)�)�)�����w�'�'�-�-�/�/��+�F�3�3�3��� � � ����>� �L� � � � �t�t�t�t�t������ � � ����D�a�H�H�H��t�t�t�t�t��������s% � A C � D7� D� D7�D2�2D7c � � K � t | � � � d{V ��}|r|S n2# t $ r%}t � d|� � Y d}~nd}~ww xY wt � d� � t | � � � d{V ��S )z� Get the version of the imunify-security wp plugin installed on given WordPress site. First tries to parse the version from the plugin file, then falls back to WP CLI. Nz,Failed to parse version from plugin file: %sz/Plugin version not found in file, trying WP CLI)r@ r5 r6 r rL ra )r* r>