@@ -63,89 +63,100 @@ public static function getScriptPath(?int $pid = null): ?string
6363 }
6464 }
6565
66- if (!function_exists ('shell_exec ' ) || (ini_get ('disable_functions ' ) && strpos (ini_get ('disable_functions ' ), 'shell_exec ' ) !== false )) {
66+ if (
67+ !function_exists ('shell_exec ' ) ||
68+ (ini_get ('disable_functions ' ) && strpos (ini_get ('disable_functions ' ), 'shell_exec ' ) !== false )
69+ ) {
6770 return null ;
6871 }
6972
7073 $ os = strtoupper (substr (PHP_OS , 0 , 3 ));
7174
7275 if ($ os === 'LIN ' ) {
73- $ cmdline_path = "/proc/ $ pid/cmdline " ;
76+ $ cmdlinePath = "/proc/ $ pid/cmdline " ;
77+ if (!is_readable ($ cmdlinePath )) {
78+ return null ;
79+ }
7480
75- if (file_exists ($ cmdline_path )) {
76- $ cmdline_content = file_get_contents ($ cmdline_path );
81+ $ args = array_values (array_filter (explode ("\0" , file_get_contents ($ cmdlinePath ))));
7782
78- $ args = explode ("\0" , $ cmdline_content );
79- $ args = array_filter ($ args );
83+ foreach ($ args as $ arg ) {
84+ if ($ arg [0 ] === '- ' ) {
85+ continue ;
86+ }
8087
81- if (isset ($ args [1 ])) {
82- $ script_path = $ args [1 ];
88+ if (substr ($ arg , -4 ) !== '.php ' ) {
89+ continue ;
90+ }
8391
84- if (realpath ( $ script_path ) !== false ) {
85- return realpath ($ script_path );
86- }
92+ if (is_file ( $ arg ) ) {
93+ return realpath ($ arg );
94+ }
8795
88- $ cwd_path = "/proc/ $ pid/cwd " ;
89- if (is_link ($ cwd_path ) && $ cwd = readlink ($ cwd_path )) {
90- $ resolved_path = realpath ($ cwd .'/ ' .$ script_path );
91- return $ resolved_path ?: $ script_path ;
96+ $ cwdPath = "/proc/ $ pid/cwd " ;
97+ if (is_link ($ cwdPath )) {
98+ $ cwd = readlink ($ cwdPath );
99+ if ($ cwd && is_file ($ cwd . '/ ' . $ arg )) {
100+ return realpath ($ cwd . '/ ' . $ arg );
92101 }
93-
94- return $ script_path ;
95102 }
96103 }
97104
98105 return null ;
99106 }
100107
101108 if ($ os === 'WIN ' ) {
102- $ command = "powershell -NoProfile -Command \"(Get-CimInstance -ClassName Win32_Process -Filter \"ProcessId=' $ pid' \").CommandLine \" 2>&1 " ;
109+ $ command = "powershell -NoProfile -Command \"(Get-CimInstance Win32_Process -Filter \\\"ProcessId=' $ pid' \\\").CommandLine \" 2>&1 " ;
110+ $ output = trim ((string ) shell_exec ($ command ));
103111
104- $ output = shell_exec ($ command );
112+ if ($ output === '' ) {
113+ return null ;
114+ }
105115
106- if (! empty ( $ output )) {
107- $ full_command_line = trim ( $ output );
116+ preg_match_all ( ' /"([^"]+)"|(\S+)/ ' , $ output , $ matches );
117+ $ args = array_values ( array_filter ( array_merge ( $ matches [ 1 ], $ matches [ 2 ])) );
108118
109- if (preg_match ('/^"(.+?)"\s*"(.+?)"/ ' , $ full_command_line , $ script_matches )) {
110- $ script_path = $ script_matches [2 ];
111- return $ script_path ;
112- } else {
113- $ parts = explode (' ' , $ full_command_line , 3 );
114- if (isset ($ parts [1 ])) {
115- $ script_path = trim ($ parts [1 ], '" ' );
116- return $ script_path ;
117- }
119+ foreach ($ args as $ arg ) {
120+ if ($ arg [0 ] === '- ' ) {
121+ continue ;
122+ }
123+
124+ if (substr ($ arg , -4 ) !== '.php ' ) {
125+ continue ;
126+ }
127+
128+ if (is_file ($ arg )) {
129+ return realpath ($ arg );
118130 }
119131 }
120132
121133 return null ;
122-
123134 }
124135
125136 if (PHP_OS_FAMILY === 'Darwin ' ) {
126- if (!function_exists ('shell_exec ' )) {
137+ $ commandLine = trim ((string ) shell_exec ("ps -p $ pid -o command= 2>/dev/null " ));
138+ if ($ commandLine === '' ) {
127139 return null ;
128140 }
129141
130- $ commandLine = trim (shell_exec ("ps -p $ pid -o command= 2>/dev/null " ));
142+ preg_match_all ('/"([^"]+)"| \'([^ \']+) \'|(\S+)/ ' , $ commandLine , $ matches );
143+ $ args = array_values (array_filter (array_merge ($ matches [1 ], $ matches [2 ], $ matches [3 ])));
131144
132- if ($ commandLine !== '' ) {
133- preg_match_all ('/"([^"]+)"| \'([^ \']+) \'|(\S+)/ ' , $ commandLine , $ matches );
134- $ args = array_filter (array_merge ($ matches [1 ], $ matches [2 ], $ matches [3 ]));
135-
136- if (isset ($ args [1 ])) {
137- $ script = $ args [1 ];
145+ foreach ($ args as $ arg ) {
146+ if ($ arg [0 ] === '- ' ) {
147+ continue ;
148+ }
138149
139- if (realpath ( $ script ) ) {
140- return realpath ( $ script ) ;
141- }
150+ if (substr ( $ arg , - 4 ) !== ' .php ' ) {
151+ continue ;
152+ }
142153
143- return $ script ;
154+ if (is_file ($ arg )) {
155+ return realpath ($ arg );
144156 }
145157 }
146158
147- $ binaryPath = trim (shell_exec ("proc_pidpath $ pid 2>/dev/null " ));
148- return $ binaryPath !== '' ? $ binaryPath : null ;
159+ return null ;
149160 }
150161
151162 return null ;
0 commit comments