1
0
Fork 0

Merge pull request #9713 from Seldaek/fix-unixy-proxy

Generate binary proxy in PHP if the target binary is detected as a PHP script
pull/9735/head
Jordi Boggiano 2021-02-23 14:15:44 +01:00 committed by GitHub
commit cf2128a889
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 0 deletions

View File

@ -191,6 +191,42 @@ class BinaryInstaller
$binDir = ProcessExecutor::escape(dirname($binPath)); $binDir = ProcessExecutor::escape(dirname($binPath));
$binFile = basename($binPath); $binFile = basename($binPath);
$binContents = file_get_contents($bin);
if (preg_match('{^(?:#!(?:/usr)?/bin/env php|#!(?:/usr)?/bin/php|<?php)\r?\n}', $binContents, $match)) {
$proxyCode = trim($match[0]);
// carry over the existing shebang if present, otherwise add our own
if ($proxyCode === "<?php") {
$proxyCode = "#!/usr/bin/env php";
}
$binPathExported = var_export($binPath, true);
return $proxyCode . "\n" . <<<PROXY
<?php
/**
* Proxy PHP file generated by Composer
*
* This file includes the referenced bin path ($binPath) using eval to remove the shebang if present
*
* @generated
*/
\$binPath = realpath(__DIR__ . "/" . $binPathExported);
\$contents = file_get_contents(\$binPath);
\$contents = preg_replace('{^#!/.+\\r?\\n<\\?(php)?}', '', \$contents, 1, \$replaced);
if (\$replaced) {
\$contents = strtr(\$contents, array(
'__FILE__' => var_export(\$binPath, true),
'__DIR__' => var_export(dirname(\$binPath), true),
));
eval(\$contents);
exit(0);
}
include \$binPath;
PROXY;
}
$proxyCode = <<<PROXY $proxyCode = <<<PROXY
#!/usr/bin/env sh #!/usr/bin/env sh