Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Mongoose environment variables (REQUEST_URI, PHP_SELF) #137

Closed
GoogleCodeExporter opened this issue Aug 28, 2015 · 9 comments
Closed
Labels

Comments

@GoogleCodeExporter
Copy link

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

@GoogleCodeExporter
Copy link
Author

Issue 112 has been merged into this issue.

Original comment by czarek.t...@gmail.com on 27 Oct 2014 at 7:49

@GoogleCodeExporter
Copy link
Author

Added pretty-urls.php example in revision 6e7bda97504f. See:
https://code.google.com/p/phpdesktop/source/browse/phpdesktop-chrome/www/pretty-
urls.php

Original comment by czarek.t...@gmail.com on 27 Oct 2014 at 7:56

  • Changed title: Fix Mongoose environment variables (REQUEST_URI, PHP_SELF)
  • Added labels: NextRelease, Priority-High
  • Removed labels: Priority-Medium

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

The PHP_SELF fix in __fix_mongoose_env_variables wasn't correct. PHP_SELF 
should contain PATH_INFO, eg:

  [PHP_SELF] => /test.php/foo/bar
  [SCRIPT_NAME] => /test.php

See for reference:
http://stackoverflow.com/questions/279966/php-self-vs-path-info-vs-script-name-v
s-request-uri

Original comment by czarek.t...@gmail.com on 29 Oct 2014 at 9:27

@GoogleCodeExporter
Copy link
Author

GoogleCodeExporter commented Aug 28, 2015

Fixed in revision 39c81a7.

@GoogleCodeExporter
Copy link
Author

Project will move to Github. Find this issue at the new address (soon): 
https://github.com/cztomczak/phpdesktop/issues/137

Original comment by czarek.t...@gmail.com on 24 Aug 2015 at 3:33

@kamdjouduplex
Copy link

Hi there, I know this bug have been discussed a long time ago but I really need a help.

I am facing the same problem of url redirection in my fresh intall laravel 5.6 on phpdesktop-chrome-57.0-rc-php-7.1.3.

Everything works fine when you launch the app. but when I start adding links to new pages, the redirection on click is keeping throwing this Error 404: Not Found File not found . and the same error in the console. but the route and file existe I don't know what to do.

I have try to follow of the solution discussed here. but not working. I which to know exactly where I have to insert the __fix_mongoose_env_variables(){ ...} in my laravel app.

@cztomczak
Copy link
Owner

Best to ask on Laravel support channel on where to put this code.

@cztomczak
Copy link
Owner

cztomczak commented Jul 27, 2018

That function __fix_mongoose_env_variables should not be needed anymore, as the mongoose env variables issue was fixed in revision 39c81a7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants