Closed
Description
Mongoose environment variables are broken in a few ways:
* REQUEST_URI does not contain QUERY_STRING
* Pretty urls like "/index.php/company/5" have invalid values for SCRIPT_NAME and PHP_SELF keys
* SCRIPT_FILENAME and PATH_TRANSLATED contain forward slash before script name
When accessing url like "/pretty-urls.php/company/5" keys in $_SERVER variable
are:
Array
(
[REQUEST_URI] => /pretty-urls.php/company/5
[SCRIPT_NAME] => /pretty-urls.php/company/pretty-urls.php
[SCRIPT_FILENAME] => C:\phpdesktop\phpdesktop-chrome\www/pretty-urls.php
[PATH_TRANSLATED] => C:\phpdesktop\phpdesktop-chrome\www/pretty-urls.php
[PATH_INFO] => /company/5
[HTTP_REFERER] => http://127.0.0.1:49257/pretty-urls.php
[PHP_SELF] => /pretty-urls.php/company/pretty-urls.php/company/5
)
Before a final fix is released, you can use the PHP function below to fix these
issues. Apply this code at the top of PHP script:
<?php
function __fix_mongoose_env_variables()
{
// REQUEST_URI does not contain QUERY_STRING. See Issue 112
// in the tracker. The condition below will be always executed.
if (strpos($_SERVER["REQUEST_URI"], "?") === false) {
// Fix PHP_SELF and SCRIPT_NAME env variables which may be
// broken for pretty urls like "/index.php/company/5":
// >> [PHP_SELF] => /index.php/company/index.php/company/5
$php_self = $_SERVER["PHP_SELF"];
if (strrpos($php_self, "/") !== 0) {
// When PHP_SELF contains more than one slash. Remove
// path info from both PHP_SELF and SCRIPT_NAME.
$php_self = preg_replace('#^(/+[^/\\\]+)[\s\S]+#',
'\\1', $php_self);
$_SERVER["PHP_SELF"] = $php_self;
$_SERVER["SCRIPT_NAME"] = $php_self;
}
// Append QUERY_STRING to REQUEST_URI env variable.
if (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"]) {
$_SERVER["REQUEST_URI"] = $_SERVER["REQUEST_URI"]."?".(
$_SERVER["QUERY_STRING"]);
}
}
// Fix forward slash in SCRIPT_FILENAME and PATH_TRANSLATED:
// >> C:\phpdesktop\phpdesktop-chrome\www/pretty-urls.php
// should become:
// >> C:\phpdesktop\phpdesktop-chrome\www\pretty-urls.php
$_SERVER["SCRIPT_FILENAME"] = str_replace("/", "\\",
$_SERVER["SCRIPT_FILENAME"]);
$_SERVER["PATH_TRANSLATED"] = str_replace("/", "\\",
$_SERVER["PATH_TRANSLATED"]);
// Fixes were applied to $_SERVER only. Apply them to $_ENV as well.
$keys_to_fix = ["REQUEST_URI", "SCRIPT_NAME", "PHP_SELF",
"SCRIPT_FILENAME", "PATH_TRANSLATED"];
foreach ($keys_to_fix as $env_key) {
putenv("$env_key={$_SERVER[$env_key]}");
$_ENV[$env_key] = $_SERVER[$env_key];
}
}
__fix_mongoose_env_variables();
?>
Original issue reported on code.google.com by czarek.t...@gmail.com
on 27 Oct 2014 at 7:47