USE $_SERVER['SCRIPT_FILENAME'] instead of getenv() - #5720
Conversation
|
Okay, I didn't expect the PHPMD error. Let me find another way. |
e6a353e to
e5a054a
Compare
There was a problem hiding this comment.
Pull request overview
This PR improves API routing and URL generation by replacing getenv('SCRIPT_FILENAME') with $_SERVER['SCRIPT_FILENAME'], addressing cases where modern runtimes (e.g., FrankenPHP worker mode) do not expose per-request values via environment variables.
Changes:
- Use
$_SERVER['SCRIPT_FILENAME']to derive the API2 route prefix (avoidsgetenv()returning empty/incorrect values). - Use
$_SERVER['SCRIPT_FILENAME']when building SOAP service URLs inMage_Api_Helper_Data. - Remove now-unneeded PHPStan baseline ignores related to
basename(getenv(...))potentially returningfalse.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| app/code/core/Mage/Api2/Model/Route/ApiType.php | Switches API type route prefix derivation from getenv() to $_SERVER. |
| app/code/core/Mage/Api/Helper/Data.php | Switches script name derivation for service URL path building from getenv() to $_SERVER. |
| .phpstan.dist.baselines/argument.type.php | Removes baseline entries that are no longer applicable after the change. |
25479b8 to
5f85a95
Compare
|
I added the changes suggested by Copilot. |
5f85a95 to
86f88f0
Compare
|
|
Fixed the rector warning too |



Description (*)
This change replaces
getenv('SCRIPT_FILENAME')with$_SERVER['SCRIPT_FILENAME'].Otherwise, if you try to access the REST API using FrankenPHP as the web server, you will receive error 400: 'API type "api" is not supported'.
In app/code/core/Mage/Api2/Model/Route/ApiType.php, OpenMage uses
getenv('SCRIPT_FILENAME')to read the script filename from an environment variable.This works with "classic" webservers, such as Apache or nginx and the PHP-FPM runtime. These servers set variables such as SCRIPT_FILENAME as both environment variables and in the $_SERVER superglobal. More modern PHP runtimes, such as FrankenPHP only expose these variables in the $_SERVER superglobal and not as environment variables. As far as i understand, this is done for better isolation in FrankenPHP's worker mode. An environment variable would be visible to all PHP requests within the same worker process, whereas the
$_SERVERsuperglobal is set individually set for each request.Therefore,
getenv('SCRIPT_FILENAME')can return incorrect or empty values on these modern runtimes. It's safer to use$_SERVER['SCRIPT_FILENAME'], which works on all runtimes (including Apache, nginx/FPM, Caddy/FPM, and FrankenPHP).Manual testing scenarios (*)
Unauthorizedor 403Access denied, because you are not sending access tokens.Contribution checklist (*)