@@ -15,41 +15,6 @@ public function __construct() {
1515 @mkdir (self ::TMPDIR_DOWNLOADS );
1616 }
1717
18- public function run (string $ cmd , $ args , &$ cmd_result = null ): bool {
19- $ cli_script = realpath (HESTIA_DIR_BIN . $ cmd );
20- if (!str_starts_with ((string ) $ cli_script , HESTIA_DIR_BIN )) {
21- $ errstr = "$ cmd is trying to traverse outside of " . HESTIA_DIR_BIN ;
22- trigger_error ($ errstr );
23- throw new \Exception ($ errstr );
24- }
25- $ cli_script = "/usr/bin/sudo " . quoteshellarg ($ cli_script );
26-
27- $ cli_arguments = "" ;
28- if (!empty ($ args ) && is_array ($ args )) {
29- foreach ($ args as $ arg ) {
30- $ cli_arguments .= quoteshellarg ((string ) $ arg ) . " " ;
31- }
32- } else {
33- $ cli_arguments = quoteshellarg ($ args );
34- }
35-
36- exec ($ cli_script . " " . $ cli_arguments . " 2>&1 " , $ output , $ exit_code );
37-
38- $ result ["code " ] = $ exit_code ;
39- $ result ["args " ] = $ cli_arguments ;
40- $ result ["raw " ] = $ output ;
41- $ result ["text " ] = implode (PHP_EOL , $ output );
42- $ result ["json " ] = json_decode ($ result ["text " ], true );
43- $ cmd_result = (object ) $ result ;
44- if ($ exit_code > 0 ) {
45- //log error message in nginx-error.log
46- trigger_error ($ result ["text " ]);
47- //throw exception if command fails
48- throw new \Exception ($ result ["text " ]);
49- }
50- return $ exit_code === 0 ;
51- }
52-
5318 public function runUser (string $ cmd , $ args , &$ cmd_result = null ): bool {
5419 if (!empty ($ args ) && is_array ($ args )) {
5520 array_unshift ($ args , $ this ->user ());
@@ -364,4 +329,39 @@ public function cleanupTmpDir(): void {
364329 public function __destruct () {
365330 $ this ->cleanupTmpDir ();
366331 }
332+
333+ private function run (string $ cmd , $ args , &$ cmd_result = null ): bool {
334+ $ cli_script = realpath (HESTIA_DIR_BIN . $ cmd );
335+ if (!str_starts_with ((string ) $ cli_script , HESTIA_DIR_BIN )) {
336+ $ errstr = "$ cmd is trying to traverse outside of " . HESTIA_DIR_BIN ;
337+ trigger_error ($ errstr );
338+ throw new \Exception ($ errstr );
339+ }
340+ $ cli_script = "/usr/bin/sudo " . quoteshellarg ($ cli_script );
341+
342+ $ cli_arguments = "" ;
343+ if (!empty ($ args ) && is_array ($ args )) {
344+ foreach ($ args as $ arg ) {
345+ $ cli_arguments .= quoteshellarg ((string ) $ arg ) . " " ;
346+ }
347+ } else {
348+ $ cli_arguments = quoteshellarg ($ args );
349+ }
350+
351+ exec ($ cli_script . " " . $ cli_arguments . " 2>&1 " , $ output , $ exit_code );
352+
353+ $ result ["code " ] = $ exit_code ;
354+ $ result ["args " ] = $ cli_arguments ;
355+ $ result ["raw " ] = $ output ;
356+ $ result ["text " ] = implode (PHP_EOL , $ output );
357+ $ result ["json " ] = json_decode ($ result ["text " ], true );
358+ $ cmd_result = (object ) $ result ;
359+ if ($ exit_code > 0 ) {
360+ //log error message in nginx-error.log
361+ trigger_error ($ cli_script . " " . $ cli_arguments . " | " . $ result ["text " ]);
362+ //throw exception if command fails
363+ throw new \Exception ($ result ["text " ]);
364+ }
365+ return $ exit_code === 0 ;
366+ }
367367}
0 commit comments