sapioit / phpdesktop

Automatically exported from code.google.com/p/phpdesktop
0 stars 0 forks source link

Fix Mongoose environment variables (REQUEST_URI, PHP_SELF) #137

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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 commented 8 years ago
Issue 112 has been merged into this issue.

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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
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 commented 8 years ago
Fixed in revision 39c81a7b2b24.

Original comment by czarek.t...@gmail.com on 29 Oct 2014 at 10:38

GoogleCodeExporter commented 8 years ago
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