hp|dZddlZddlZddlZddlmZddlmZddlm Z m Z m Z ddl m Z mZddlmZmZmZddlmZdd lmZejeZd ed efd Zd ed efdZded efdZdefdZdefdZ defdZ!defdZ"defdZ#defdZ$defdZ%defdZ&e ddefdZ'dZ(defdZ)dS)u  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program.  If not, see . Copyright © 2019 Cloud Linux Software Inc. This software is also available under ImunifyAV commercial license, see 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 G/opt/imunify360/venv/lib/python3.11/site-packages/imav/wordpress/cli.py_validate_semverr-su 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@ks(--------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@r5r6rrLr_)r*r>r?s rget_plugin_versionrasJ7========  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_installedrd s|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.Ncorercz#Checking if WordPress is installed FT) rGrHrIrJr r rKr r6rLrrrMs ris_wordpress_installedrg#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 rZz0Failed 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_directoryrj:s\ |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_dirrj)r*r8wp_content_dirs rr.r.Yst|$$|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_cachertqs!!!!!r)cKt|d{V}|st|jdz }t|dz S)zO Get the Imunify Security data directory for the given WordPress site. Nrnr-)r.rrK)r*r8s r get_data_dirrvvsW(--------K 84<((<7   1 11r))*__doc__loggingrGr0pathlibrdistutils.versionrdefence360agent.utilsrrrimav.wordpressrr imav.wordpress.utilsr r r defence360agent.sentryr imav.model.wordpressr getLogger__name__r6rboolrr(r@rRrTrWr_rardrgrjr.rtrvrsr)rrs* ++++++ 43333333 /.....''''''  8 $ $#$$))))))X&&3&&&&Rv*f2(FD+6++++&F0v.v>.""" 2V222222r)