@@ -32,6 +32,66 @@ function xml_decode($string) {
3232 return strval (html_entity_decode ($ string , ENT_XML1 , 'UTF-8 ' ));
3333}
3434
35+ function extraParamsWithQuotedValuesMasked ($ extraParams ) {
36+ return preg_replace ('/"[^" \\\\]*(?: \\\\.[^" \\\\]*)*"| \'[^ \']* \'/ ' , '"" ' , $ extraParams );
37+ }
38+
39+ function replaceUnquotedExtraParams ($ extraParams , $ callback ) {
40+ $ parts = preg_split ('/("[^" \\\\]*(?: \\\\.[^" \\\\]*)*"| \'[^ \']* \')/ ' , $ extraParams , -1 , PREG_SPLIT_DELIM_CAPTURE );
41+ if ($ parts === false ) {
42+ return $ extraParams ;
43+ }
44+ foreach ($ parts as $ i => $ part ) {
45+ if ($ part === '' || $ part [0 ] === '" ' || $ part [0 ] === "' " ) {
46+ continue ;
47+ }
48+ $ parts [$ i ] = $ callback ($ part );
49+ }
50+ return implode ('' , $ parts );
51+ }
52+
53+ function extractMacAddressParam ($ extraParams ) {
54+ if (!is_string ($ extraParams )) {
55+ return '' ;
56+ }
57+ $ extraParams = extraParamsWithQuotedValuesMasked ($ extraParams );
58+ if (preg_match ('/(?:^|\s)--mac-address=([^\s \'"]+)/ ' , $ extraParams , $ match )) {
59+ return trim ($ match [1 ]);
60+ }
61+ if (preg_match ('/(?:^|\s)--mac-address\s+([^\s \'"]+)/ ' , $ extraParams , $ match )) {
62+ return trim ($ match [1 ]);
63+ }
64+ return '' ;
65+ }
66+
67+ function removeMacAddressParam ($ extraParams ) {
68+ if (!is_string ($ extraParams ) || $ extraParams === '' ) {
69+ return '' ;
70+ }
71+ $ extraParams = replaceUnquotedExtraParams ($ extraParams , function ($ part ) {
72+ $ part = preg_replace ('/(^|\s)--mac-address=[^\s \'"]+/ ' , '$1 ' , $ part );
73+ return preg_replace ('/(^|\s)--mac-address\s+[^\s \'"]+/ ' , '$1 ' , $ part );
74+ });
75+ return trim ($ extraParams );
76+ }
77+
78+ function hasNetworkParam ($ extraParams ) {
79+ return is_string ($ extraParams ) && preg_match ('/(?:^|\s)--net(?:work)?(?:=|\s+)[^\s \'"]+/ ' , extraParamsWithQuotedValuesMasked ($ extraParams ));
80+ }
81+
82+ function normalizeMacAddress ($ mac ) {
83+ $ mac = strtolower (trim ($ mac ?? '' ));
84+ if ($ mac === '' ) {
85+ return '' ;
86+ }
87+ if (preg_match ('/^[0-9a-f]{12}$/ ' , $ mac )) {
88+ $ mac = implode (': ' , str_split ($ mac , 2 ));
89+ } else {
90+ $ mac = str_replace ('- ' , ': ' , $ mac );
91+ }
92+ return preg_match ('/^([0-9a-f]{2}:){5}[0-9a-f]{2}$/ ' , $ mac ) ? $ mac : '' ;
93+ }
94+
3595function generateTSwebui ($ url , $ serve , $ webUI ) {
3696 if (!isset ($ webUI )) {
3797 return '' ;
@@ -75,6 +135,9 @@ function postToXML($post, $setOwnership=false) {
75135 $ xml ->Network = xml_encode ($ post ['contNetwork ' ]);
76136 }
77137 $ xml ->MyIP = xml_encode ($ post ['contMyIP ' ]);
138+ $ extraNetwork = hasNetworkParam ($ post ['contExtraParams ' ] ?? '' );
139+ $ myMAC = $ extraNetwork ? '' : normalizeMacAddress (trim ($ post ['contMyMAC ' ] ?? '' ) ?: extractMacAddressParam ($ post ['contExtraParams ' ] ?? '' ));
140+ $ xml ->MyMAC = xml_encode ($ myMAC );
78141 $ xml ->Shell = xml_encode ($ post ['contShell ' ]);
79142 $ xml ->Privileged = strtolower ($ post ['contPrivileged ' ]??'' )=='on ' ? 'true ' : 'false ' ;
80143 $ xml ->Support = xml_encode ($ post ['contSupport ' ]);
@@ -85,7 +148,7 @@ function postToXML($post, $setOwnership=false) {
85148 $ xml ->WebUI = xml_encode (trim ($ post ['contWebUI ' ]));
86149 $ xml ->TemplateURL = xml_encode ($ post ['contTemplateURL ' ]);
87150 $ xml ->Icon = xml_encode (trim ($ post ['contIcon ' ]));
88- $ xml ->ExtraParams = xml_encode ($ post ['contExtraParams ' ]);
151+ $ xml ->ExtraParams = xml_encode ($ myMAC && ! $ extraNetwork ? removeMacAddressParam ( $ post [ ' contExtraParams ' ]) : $ post ['contExtraParams ' ]);
89152 $ xml ->PostArgs = xml_encode ($ post ['contPostArgs ' ]);
90153 $ xml ->CPUset = xml_encode ($ post ['contCPUset ' ]);
91154 $ xml ->DateInstalled = xml_encode (time ());
@@ -149,6 +212,9 @@ function xmlToVar($xml) {
149212 $ out ['Registry ' ] = xml_decode ($ xml ->Registry );
150213 $ out ['Network ' ] = xml_decode ($ xml ->Network );
151214 $ out ['MyIP ' ] = xml_decode ($ xml ->MyIP ?? '' );
215+ $ extraParams = xml_decode ($ xml ->ExtraParams ?? '' );
216+ $ extraNetwork = hasNetworkParam ($ extraParams );
217+ $ out ['MyMAC ' ] = $ extraNetwork ? '' : normalizeMacAddress (xml_decode ($ xml ->MyMAC ?? '' ) ?: extractMacAddressParam ($ extraParams ));
152218 $ out ['Shell ' ] = xml_decode ($ xml ->Shell ?? 'sh ' );
153219 $ out ['Privileged ' ] = xml_decode ($ xml ->Privileged );
154220 $ out ['Support ' ] = xml_decode ($ xml ->Support );
@@ -159,7 +225,7 @@ function xmlToVar($xml) {
159225 $ out ['WebUI ' ] = xml_decode ($ xml ->WebUI );
160226 $ out ['TemplateURL ' ] = xml_decode ($ xml ->TemplateURL );
161227 $ out ['Icon ' ] = xml_decode ($ xml ->Icon );
162- $ out ['ExtraParams ' ] = xml_decode ( $ xml -> ExtraParams ) ;
228+ $ out ['ExtraParams ' ] = $ extraParams ;
163229 $ out ['PostArgs ' ] = xml_decode ($ xml ->PostArgs );
164230 $ out ['CPUset ' ] = xml_decode ($ xml ->CPUset );
165231 $ out ['DonateText ' ] = xml_decode ($ xml ->DonateText );
@@ -325,13 +391,29 @@ function xmlToCommand($xml, $create_paths=false) {
325391 $ xml = xmlToVar ($ xml );
326392 $ cmdName = strlen ($ xml ['Name ' ]) ? '--name= ' .escapeshellarg ($ xml ['Name ' ]) : '' ;
327393 $ cmdPrivileged = strtolower ($ xml ['Privileged ' ])=='true ' ? '--privileged=true ' : '' ;
394+ $ extraNetwork = hasNetworkParam ($ xml ['ExtraParams ' ]);
395+ $ cmdMyIP = '' ;
328396 if (preg_match ('/^container:(.*)/ ' , $ xml ['Network ' ])) {
329- $ cmdNetwork = preg_match ( ' /\-\-net(work)?=/ ' , $ xml [ ' ExtraParams ' ]) ? "" : '--net= ' .escapeshellarg ($ xml ['Network ' ]);
397+ $ cmdNetwork = $ extraNetwork ? "" : '--net= ' .escapeshellarg ($ xml ['Network ' ]);
330398 } else {
331- $ cmdNetwork = preg_match ('/\-\-net(work)?=/ ' ,$ xml ['ExtraParams ' ]) ? "" : '--net= ' .escapeshellarg (strtolower ($ xml ['Network ' ]));
399+ $ networkName = strtolower ($ xml ['Network ' ]);
400+ if ($ extraNetwork ) {
401+ $ cmdNetwork = "" ;
402+ } elseif (strlen ($ xml ['MyMAC ' ]) && !in_array ($ networkName , ['host ' ,'none ' ])) {
403+ $ xml ['ExtraParams ' ] = removeMacAddressParam ($ xml ['ExtraParams ' ]);
404+ $ networkEndpoint = ['name= ' .$ networkName ];
405+ foreach (explode (' ' ,str_replace (', ' ,' ' ,$ xml ['MyIP ' ])) as $ myIP ) {
406+ if ($ myIP ) $ networkEndpoint [] = (strpos ($ myIP ,': ' ) !== false ? 'ip6= ' : 'ip= ' ).$ myIP ;
407+ }
408+ $ networkEndpoint [] = 'mac-address= ' .$ xml ['MyMAC ' ];
409+ $ cmdNetwork = '--network= ' .escapeshellarg (implode (', ' , $ networkEndpoint ));
410+ } else {
411+ $ cmdNetwork = '--net= ' .escapeshellarg ($ networkName );
412+ }
413+ }
414+ if (!strlen ($ xml ['MyMAC ' ]) || preg_match ('/^container:(.*)/ ' , $ xml ['Network ' ]) || $ extraNetwork ) {
415+ foreach (explode (' ' ,str_replace (', ' ,' ' ,$ xml ['MyIP ' ])) as $ myIP ) if ($ myIP ) $ cmdMyIP .= (strpos ($ myIP ,': ' ) !== false ? '--ip6= ' : '--ip= ' ).escapeshellarg ($ myIP ).' ' ;
332416 }
333- $ cmdMyIP = '' ;
334- foreach (explode (' ' ,str_replace (', ' ,' ' ,$ xml ['MyIP ' ])) as $ myIP ) if ($ myIP ) $ cmdMyIP .= (strpos ($ myIP ,': ' )?'--ip6= ' :'--ip= ' ).escapeshellarg ($ myIP ).' ' ;
335417 $ cmdCPUset = strlen ($ xml ['CPUset ' ]) ? '--cpuset-cpus= ' .escapeshellarg ($ xml ['CPUset ' ]) : '' ;
336418 $ Volumes = ['' ];
337419 $ Ports = ['' ];
0 commit comments