seanmorris / php-wasm

PHP in Browser, powered by WebAssembly.
https://seanmorris.github.io/php-wasm/
Apache License 2.0
744 stars 38 forks source link

SCRIPT_NAME and PHP_SELF are always vHost entrypoint even if using specific PHP file #62

Open mglaman opened 4 weeks ago

mglaman commented 4 weeks ago

I set up a file called baseurl.php to debug Symfony request object:

<?php

use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once 'autoload.php';

$request = Request::createFromGlobals();
var_export([
  'SCRIPT_FILENAME' => $request->server->get('SCRIPT_FILENAME'),
  'SCRIPT_NAME' => $request->server->get('SCRIPT_NAME'),
  'PHP_SELF' => $request->server->get('PHP_SELF'),
  'filename' => basename($request->server->get('SCRIPT_FILENAME', '')),
  'scriptname' => basename($request->server->get('SCRIPT_NAME', '')),
]);
print PHP_EOL;
var_export($request->getUri());
print PHP_EOL;
var_export($request->getBaseUrl());
print PHP_EOL;
var_export($request->getBasePath());
print PHP_EOL;

The path is /cgi/drupal/baseurl.php

The output is

array (
  'SCRIPT_FILENAME' => '/persist/drupal/web//baseurl.php',
  'SCRIPT_NAME' => '/cgi/drupal/index.php',
  'PHP_SELF' => '/cgi/drupal/index.php',
  'filename' => 'baseurl.php',
  'scriptname' => 'index.php',
)

The script name is always overwritten when it shouldn't be.

        if(vHostEntrypoint)
        {
            scriptName = vHostPrefix + '/' + vHostEntrypoint;
        }

~This should only happen if the path is rewritten to the docroot index.php.~ this worked only when going to a specific file

            // Rewrite to index
            path = docroot + '/index.php';
mglaman commented 3 weeks ago

This hack seemed to fix it

        let originalPath = url.pathname;

        const extension = path.split('.').pop();

        if(vHostEntrypoint)
        {
            if (extension === 'php') {
                scriptName = vHostPrefix + '/' + rewrite.substr((vHostPrefix || this.prefix).length).replace(/^\/+/, '')
            } else {
                scriptName = vHostPrefix + '/' + vHostEntrypoint;
            }
        }
seanmorris commented 6 days ago

Hmm, that's annoying. Let me go over the code and I'll get a fix going.