~>ddlZddlZddlZddlmZddlmZddlmZm Z m Z ddl m Z m Z ddlmZmZmZddlmZddlmZejeZd ed efd Zd ed efd Zded efdZdefdZdefdZdefdZ ded efdZ!defdZ"defdZ#defdZ$defdZ%defdZ&e ddefdZ'dZ(defdZ)dS)N)Path) StrictVersion) check_run CheckRunErrorasync_lru_cache) PLUGIN_PATH PLUGIN_SLUG)build_command_for_userget_php_binary_path wp_wrapper) log_message)WPSite version_strreturnct|tsdS|}|sdS t|dS#t$rYdSwxYw)z1Validate if a string is a valid semantic version.FT) isinstancestrstripr ValueError)r trimmed_strs R/opt/imunify360/venv/lib/python3.11/site-packages/defence360agent/wordpress/cli.py_validate_semverrsu k3 ' 'u##%%K uk"""t uusA AAoutputcb|sdS|}|sdSt|dkr+|d}t|r|St|dkr+|d}t|r|St 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 NrzMFailed to extract valid semver version from WP CLI output. Output: '{output}'rwarning wordpressz#wp-plugin-version-extraction-failed) format_argslevel component fingerprint)splitlenrrr )rparts first_part last_parts r_extract_version_from_outputr(*s t LLNNE t 5zzA~~1X^^%% J ' '   5zzA~~"IOO%% I & &   v&9  4sitecKt|d{V}|dz dz dz }|sdStjd} t |5}|D][}||}|rB|d}t|r|ccdddSddddS\ dddn #1swxYwYn4#t$r'}t d||Yd}~dSd}~wwxYwdS)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 Npluginsimunify-securityzimunify-security.phpz\* Version:\s*([0-9.]+)rz=Failed to read plugin file to determine version number %s: %s) get_content_direxistsrecompileopensearchgroupr Exceptionloggererror) r* content_dir plugin_fileversion_patternflinematchversiones r_parse_version_from_plugin_filer@Vs(--------Ki"447MM     tj!;<Ktj|jj}t ||d{V}gt ||jddtd}t||}t d|t|d{VdS)z L   ttttt  DaHHHttttts% A C D7 D D7D22D7cK t|d{V}|r|Sn2#t$r%}td|Yd}~nd}~wwxYwtdt |d{VS)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@r5r6rrLra)r*r>r?s rget_plugin_versionrcsJ7========  N  JJJEqIIIIIIIIJ KKABBB$T** * * * * * **s A AA c`Ktj|jj}t ||d{V}gt ||jddt}t||}t d| t|d{Vn#t$rYdSwxYwdS)zMCheck if the imunify-security wp plugin is installed on given WordPress site.NrB is-installedz#Checking if wp plugin is installed FT) rGrHrIrJr r rKr r r6rLrrrMs ris_plugin_installedrfs|DH%%-H(x88888888H  Hdl + +       D %Xt44G KK?g??@@@          uu 4sB B+*B+cRKtj|jj}t ||d{V}gt ||jdd}t||}t d| t|d{Vn#t$rYdSwxYwdS)zJCheck if WordPress is installed and given site is accessible using WP CLI.Ncorerez#Checking if WordPress is installed FT) rGrHrIrJr r rKr r6rLrrrMs ris_wordpress_installedri s|DH%%-H(x88888888H  Hdl + +    D %Xt44G KK?g??@@@          uu 4sB B$#B$cDKtj|jj}t ||d{V}gt ||jdd}t||}t d| t|d{V}| d S#t$r+}td|jYd}~dSd}~wt $r&}td|Yd}~dSd}~wwxYw)z Get the content directory of the WordPress site using WP CLI. This should only be used if the default wp-content directory does not exist. Nevalzecho WP_CONTENT_DIR;zGetting content directory r\z0Failed to get content directory. Return code: %sz-Failed to decode content directory output: %s)rGrHrIrJr r rKr r6rLrr]rrr7r^r_)r*rNrOrPrQr`r?s r_get_content_directoryrl7s\ |DH%%-H(x88888888H  Hdl + +    D %Xt44G KK6W66777  ))))))))}}W%%++---  > L   ttttt  DaHHHttttts$;B<< D C,, D9DDd)maxsizecKt|jdz }|r|s&t |d{V}|rt|}|S)a Get the WordPress content directory for the given WordPress site. This function first checks if the default wp-content directory exists at the site's docroot. If the default path doesn't exist or isn't a directory, it attempts to get the actual content directory using WordPress CLI's WP_CONTENT_DIR constant. Returns: Path: The WordPress content directory path wp-contentN)rrKr/is_dirrl)r*r8wp_content_dirs rr.r.Vst|$$|3K     /{'9'9';';/5d;;;;;;;;  /~..K r)c8tdS)z.Clear the async LRU cache for get_content_dir.N)r. cache_clearr)rclear_get_content_dir_cachervns!!!!!r)cKt|d{V}|st|jdz }t|dz S)zO Get the Imunify Security data directory for the given WordPress site. Nrpr-)r.rrK)r*r8s r get_data_dirrxssW(--------K 84<((<7   1 11r))*loggingrGr0pathlibrdistutils.versionrdefence360agent.utilsrrr#defence360agent.wordpress.constantsrr defence360agent.wordpress.utilsr r r defence360agent.sentryr defence360agent.model.wordpressr getLogger__name__r6rboolrr(r@rRrTrWrYrarcrfrirlr.rvrxrur)rrs ++++++ IHHHHHHH /.....222222  8 $ $#$$))))))X&&3&&&&Rv*f2(V$FD+6++++&F0v.v>.""" 2V222222r)