Commit 0fb87b2c authored by catch's avatar catch

Issue #2213643 by sun: Remove dysfunctional drupal_override_server_variables().

parent a2b6fe7e
......@@ -347,75 +347,6 @@ function config_get_config_directory($type = CONFIG_ACTIVE_DIRECTORY) {
throw new Exception(format_string('The configuration directory type %type does not exist.', array('%type' => $type)));
}
/**
* Sets appropriate server variables needed for command line scripts to work.
*
* This function can be called by command line scripts before bootstrapping
* Drupal, to ensure that the page loads with the desired server parameters.
* This is because many parts of Drupal assume that they are running in a web
* browser and therefore use information from the global PHP $_SERVER variable
* that does not get set when Drupal is run from the command line.
*
* In many cases, the default way in which this function populates the $_SERVER
* variable is sufficient, and it can therefore be called without passing in
* any input. However, command line scripts running on a multisite installation
* (or on any installation that has settings.php stored somewhere other than
* the sites/default folder) need to pass in the URL of the site to allow
* Drupal to detect the correct location of the settings.php file. Passing in
* the 'url' parameter is also required for functions like request_uri() to
* return the expected values.
*
* Most other parameters do not need to be passed in, but may be necessary in
* some cases; for example, if \Drupal::request()->getClientIP()
* needs to return anything but the standard localhost value ('127.0.0.1'),
* the command line script should pass in the desired value via the
* 'REMOTE_ADDR' key.
*
* @param $variables
* (optional) An associative array of variables within
* \Drupal::request()->server that should be replaced. If the special element
* 'url' is provided in this array, it will be used to populate some of the
* server defaults; it should be set to the URL of the current page request,
* excluding any GET request but including the script name
* (e.g., http://www.example.com/mysite/index.php).
*
* @see conf_path()
* @see request_uri()
* @see \Symfony\Component\HttpFoundation\Request::getClientIP()
*/
function drupal_override_server_variables($variables = array()) {
$request = \Drupal::request();
$server_vars = $request->server->all();
// Allow the provided URL to override any existing values in $_SERVER.
if (isset($variables['url'])) {
$url = parse_url($variables['url']);
if (isset($url['host'])) {
$server_vars['HTTP_HOST'] = $url['host'];
}
if (isset($url['path'])) {
$server_vars['SCRIPT_NAME'] = $url['path'];
}
unset($variables['url']);
}
// Define default values for $_SERVER keys. These will be used if $_SERVER
// does not already define them and no other values are passed in to this
// function.
$defaults = array(
'HTTP_HOST' => 'localhost',
'SCRIPT_NAME' => NULL,
'REMOTE_ADDR' => '127.0.0.1',
'REQUEST_METHOD' => 'GET',
'SERVER_NAME' => NULL,
'SERVER_SOFTWARE' => NULL,
'HTTP_USER_AGENT' => NULL,
);
// Replace elements of the $_SERVER array, as appropriate.
$request->server->replace($variables + $server_vars + $defaults);
// @todo remove once conf_path() no longer uses $_SERVER.
$_SERVER = $request->server->all();
}
/**
* Initializes the PHP environment.
*/
......
......@@ -205,10 +205,6 @@ function install_state_defaults() {
'profile_info' => array(),
// An array of available installation profiles.
'profiles' => array(),
// An array of server variables that will be substituted into the global
// $_SERVER array via drupal_override_server_variables(). Used by
// non-interactive installations only.
'server' => array(),
// The server URL where the interface translation files can be downloaded.
// Tokens in the pattern will be replaced by appropriate values for the
// required translation file.
......
<?php
/**
* @file
* Definition of Drupal\system\Tests\Bootstrap\OverrideServerVariablesUnitTest.
*/
namespace Drupal\system\Tests\Bootstrap;
use Drupal\simpletest\UnitTestBase;
use Symfony\Component\HttpFoundation\Request;
/**
* Tests for overriding server variables via the API.
*/
class OverrideServerVariablesUnitTest extends UnitTestBase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Overriding server variables',
'description' => 'Test that drupal_override_server_variables() works correctly.',
'group' => 'Bootstrap',
);
}
/**
* Tests providing a direct URL to to drupal_override_server_variables().
*/
function testDrupalOverrideServerVariablesProvidedURL() {
$tests = array(
'http://example.com' => array(
'HTTP_HOST' => 'example.com',
'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
),
'http://example.com/index.php' => array(
'HTTP_HOST' => 'example.com',
'SCRIPT_NAME' => '/index.php',
),
'http://example.com/subdirectory/index.php' => array(
'HTTP_HOST' => 'example.com',
'SCRIPT_NAME' => '/subdirectory/index.php',
),
);
foreach ($tests as $url => $expected_server_values) {
$container = \Drupal::getContainer();
$request = Request::createFromGlobals();
$container->set('request', $request);
\Drupal::setContainer($container);
// Call drupal_override_server_variables() and ensure that all expected
// $_SERVER variables were modified correctly.
drupal_override_server_variables(array('url' => $url));
foreach ($expected_server_values as $key => $value) {
$this->assertIdentical(\Drupal::request()->server->get($key), $value);
$this->assertIdentical($_SERVER[$key], $value);
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment