fj3dZddlZddlZddlZddlZddlmZddlmZddl Z ddl m Z ddl mZddlmZd d lmZmZmZd d lmZd d lmZmZGd deZdS)zS This module contains classes implementing X-Ray Manager behaviour for DirectAdmin N)ChainMap)glob)gettext) phpinfo_utils) BaseManager)XRayManagerErrorXRayMissingDomainXRayManagerExit) DomainInfo)user_mode_verificationwith_fpm_reload_restrictedcveZdZdZdZdZdZdZdZddd d d d d ddddddddZ de fdZ de de fdZedefdZedefdZedefdZedefdZedefdZedefdZeed e defd!Zd"edefd#Zd$ede fd%Zd"edd&ffd' ZxZS)(DirectAdminManagerzG Class implementing an X-Ray manager behaviour for DirectAdmin z//usr/local/directadmin/custombuild/options.confz2/usr/local/directadmin/data/users/*/domains/*.confz8/usr/local/directadmin/data/users/*/domains/*.subdomainsz6/usr/local/directadmin/data/users/*/domains/*.pointerszI/usr/local/directadmin/data/users/*/domains/*.subdomains.docroot.overridez/usr/local/php54/lib/php.conf.dz/usr/local/php55/lib/php.conf.dz/usr/local/php56/lib/php.conf.dz/usr/local/php70/lib/php.conf.dz/usr/local/php71/lib/php.conf.dz/usr/local/php72/lib/php.conf.dz/usr/local/php73/lib/php.conf.dz/usr/local/php74/lib/php.conf.dz/usr/local/php80/lib/php.conf.dz/usr/local/php81/lib/php.conf.dz/usr/local/php82/lib/php.conf.dz/usr/local/php83/lib/php.conf.dz/usr/local/php84/lib/php.conf.dz/usr/local/php85/lib/php.conf.d)php54php55php56php70php71php72php73php74php80php81php82php83php84php85returnc6t|j|jS)z Get supported PHP versions :return: a chained map with basic supported versions and DirectAdmin supported versions )rVERSIONS VERSIONS_DA)selfs O/opt/cloudlinux/venv/lib64/python3.11/site-packages/xray/manager/directadmin.pysupported_versionsz%DirectAdminManager.supported_versions=s  (** *filenamecnfd} t|5}d|DcdddS#1swxYwYdS#t$rQ}|jdt |dt tdz|d}~wwxYw) z~ Read lines from file :param filename: a name of file to read :return: list of stripped lines ctd5}tj|}dddn #1swxYwY|dS)z0 Retrieve file encoding rbNencoding)openchardetdetectread)fresultr(s r%get_file_encodingz.get_file_encodingMsh%% 2 11 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2*% %s'AA  A )r,c6g|]}|Sstrip).0lines r% z5DirectAdminManager.file_readlines..Ws ??? ???r'NzFailed to read [DA conf] file)fnameerrextrazFailed to read file %s)r- readlinesOSErrorloggererrorstrr _)r$r(r3r1es ` r%file_readlinesz!DirectAdminManager.file_readlinesFsG & & & & & Qh):):)<)<=== @????? @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ Q Q Q K  =.6,/FF%4%4  5 5 5"!$<"="="HIIq P  Qs:AA  A AAAA B4#A B//B4c t}||j}d}tt ||d||ddD]\}}|\}}d|vrsdd|d d d d |v|d d d |t|<|S)z Retrieve DirectAdmin PHP settings :return: dict of format {'1': {ver, fpm}, '2': {ver, fpm}...} where '1', '2' etc is an ordinal number of a handler as it is defined in options.conf c fd|DS)z Filter PHP release|mode items in seq by marker :param seq: initial sequence :param marker: should be contained in seq item :return: all items from seq containing marker cNg|]!}|vd|v |d|"S)php#) startswith)r8lmarkers r%r:zHDirectAdminManager.php_options..inner_filter..psDJJJ!aKKEQJJq||C7H7HJ$.JJr'r5)seqrNs `r% inner_filterz4DirectAdminManager.php_options..inner_filteris5JJJJsJJJ Jr'releasemoder)startnorJ=.fpm)verrYhandler)dictrFda_options_conf enumeratezipjoinsplitrC)r$parsed_optionsoptsrPindexorQrRs r% php_optionszDirectAdminManager.php_options^s ""4#788 J J J"#ll4&C&C&2l4&@&@#B#B()+++  HE1MGT7""Ms););B)?)E)Ec)J)J!K!KMM D=#zz#r2..s5zz* r'ct}t|jD]?}tj|dd}|||<@|S)z; Retrieve main domains configuration files .confr)r\rda_domain_patternospathbasenamera)r$domainsdom_confnames r% main_domainszDirectAdminManager.main_domainss_ &&T344 % %H7##H--33G<>-#@#@#C J J J K r'ct}t|jD]}|dd}||D]o}|d}|d}|d}|dkr.|d||< ||dD]}|d||d|<`#t $rYlwxYw|S) z6 Retrieve aliases configuration files z .pointersrrVrWpointerrhrrrX)r\rda_alias_patternrarFr ) r$aliases alias_confparent_domain_namealias alias_info alias_name_typesubs r%r|zDirectAdminManager.aliasess: &&t455  J!+!1!1+!>!>q!A ,,Z88  "[[-- '] "2I%%);&B&B&B ##221>>> @ @RR9K5Q5Q5Q "11Z1133R(D "s (B55 CCct}t|jD]}||D]}tj|dd\}}tj d|}|Wtj |dd}|d|}| ||<|S) zJ Retrieve subdomains_docroot_override configuration files rVr)maxsplitz(?<=php1_select=)\d(?=&)Nz.subdomains.docroot.overriderrX)r\rda_docroot_override_patternrFurllibparseunquoteraresearchrjrkrlgroup) r$ sub_php_setsub_doc_override subdomline subdompartdataphp_select_valuedomname subdomnames r%subdomains_php_settingsz*DirectAdminManager.subdomains_php_settingss ff $T%E F F G G "112BCC G G #)<#7#7$ $ %cA 6 6! D#%9-H-1$3$3 #/!#!1!12B!C!C!I!IJh!i!ijk!lnG$.!:!:!:!:J.>.D.D.F.FK + Gr'czt}|j|j|jfD]}|||S)z Retrieve all domains and subdomains, existing on DA server, including aliases in the form of dict {domain_name: domain_config} :return: {domain_name: domain_config} including subdomains )r\rprtr|update)r$da_sitesbunchs r% all_siteszDirectAdminManager.all_sitessD66&E # #E OOE " " " "r' domain_namec  |j|}nJ#t$r=|jdd|it |t dwxYw||dtdtffd }|j}|j |p |d }| |sd }|j r\tj |d | }t||d |jd|d |j|}n5t|||d|d ||d|}|jd||j|j|S)z Retrieve information about given domain from control panel environment: PHP version, user of domain, fpm status :param domain_name: name of domain :return: a DomainInfo object zRDomain does not exist on the server or is a pointer (no task allowed for pointers)rr=zeDomain '%(domain_name)s' does not exist on this server or is a pointer (no task allowed for pointers))messageitemr cfdD} |dddS#t$rYdSwxYw)z Get config value of item (e.g. item=value) :param item: key to get value of :return: value of item c@g|]}|v|Sr5r6)r8r9rs r%r:zIDirectAdminManager.get_domain_info..find_item..s&CCCdddllTZZ\\lllr'rrVrW1)ra IndexError)rfoundrs` r% find_itemz5DirectAdminManager.get_domain_info..find_itemsb DCCCdCCCE Qx~~c**2..   ss s 3 AA php1_selectrusername)domainrJT)ropanel_php_versionphp_ini_scan_diris_selector_applieduser panel_fpmr[rZrY)rorrrr[z@Retrieved domain info: domain %s owned by %s uses php version %s)rKeyErrorrAwarningr rDrFrCrfrget phpinfo_moderget_php_configurationr get_full_php_versionabsolute_ini_scan_dir is_php_fpminforr[) r$r domain_confrrc php_selectedconfig domain_infors @r%get_domain_infoz"DirectAdminManager.get_domain_infos! _.5KK _ _ _ K  d$k2  4 4 4$K,-/]-^-^___ _  _"";// C C      377 5%Im44 xx %% L   ;"8 *%%k;;;F$ "("="=e"D"D!'!=%)Yz** +$   KK%+7;L7I%7P*3)J*?*?/3L/A%/H-9 ;;;K  N );+> @ @ @s AArcTd}|j|jd}|jdko||vS)z Check if selector is enabled specifically for DirectAdmin Required to be implemented by child classes :param domain_info: a DomainInfo object :return: True if yes, False otherwise )suphplsphpfastcgir[r)rfr[)r$rcompatible_handlerscurrent_handlers r%panel_specific_selector_enabledz2DirectAdminManager.panel_specific_selector_enabled s8<*;+>? J"c)ToAT.TTr'dom_infoc&d|jddS)z Get DirectAdmin FPM service name :param dom_info: a DomainInfo object :return: FPM service name zphp-fpmN)r)r$rs r%fpm_service_namez#DirectAdminManager.fpm_service_name+s ;3BCC8:::r'NcH tjd|jddgddnC#|j$r6}|jdt ||dYd}~nd}~wwxYw|j|jd d krx tjgd dd|j d nC#|j$r6}|jd t ||dYd}~nd}~wwxYwt |dS)z Copy xray.so for current version, create ini_location directory Reload FPM service or kill all *php* processes of user :param domain_info: a DomainInfo object z"/usr/share/alt-php-xray/da_cp_xrayrNT)capture_outputtextzFailed to copy xray.so)r<rr=r[mod_php)z/usr/sbin/servicehttpdrestartzhttpd restartedzFailed to restart httpd) subprocessrunrsubprocess_errorsrArBrCrfr[rsuperphp_procs_reload)r$rrE __class__s r%rz#DirectAdminManager.php_procs_reload3s  ; N@'9"##>@*.T ; ; ; ; ;% ; ; ; K  6,/FF-8%:%:  ; ; ; ; ; ; ; ; ;  K/ 0 ;y H H ? + + +/3????   !23333) ? ? ? !!";03A1<)>)>"???????? ?   -----s,&* A*,A%%A* 3B>> C>,C99C>) __name__ __module__ __qualname____doc__r]rirsr{rr#rr&rClistrFpropertyr\rfrprtr|rrrrr rboolrrr __classcell__)rs@r%rr!sfHOLUO"m32222222222222K"*H****QsQtQQQQ0TX@dX D   X X4X" 4   X J3J:JJJ JX U: U$ U U U U;;;;;;.J.4..........r'r)rrjrr urllib.parser collectionsrrr.xrayrrD xray.internalrbaserinternal.exceptionsr r r internal.typesr internal.user_plugin_utilsrrrr5r'r%rs4   ''''''VVVVVVVVVV'''''' k.k.k.k.k.k.k.k.k.k.r'