diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 534b647..2293aa9 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -559,10 +559,9 @@ function config_get_config_directory($type = CONFIG_ACTIVE_DIRECTORY) { * 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::service('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. + * some cases; for example, if Drupal's ip_address() function 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 $_SERVER that should @@ -573,7 +572,7 @@ function config_get_config_directory($type = CONFIG_ACTIVE_DIRECTORY) { * * @see conf_path() * @see request_uri() - * @see \Symfony\Component\HttpFoundation\Request::getClientIP() + * @see ip_address() */ function drupal_override_server_variables($variables = array()) { // Allow the provided URL to override any existing values in $_SERVER. @@ -1791,7 +1790,7 @@ function watchdog($type, $message, array $variables = NULL, $severity = WATCHDOG 'uid' => $user_uid, 'request_uri' => $base_root . request_uri(), 'referer' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', - 'ip' => Drupal::service('request')->getClientIP(), + 'ip' => ip_address(), // Request time isn't accurate for long processes, use time() instead. 'timestamp' => time(), ); @@ -2055,7 +2054,7 @@ function drupal_hash_base64($data) { function drupal_anonymous_user() { $values = array( 'uid' => 0, - 'hostname' => Drupal::service('request')->getClientIP(), + 'hostname' => ip_address(), 'roles' => array( DRUPAL_ANONYMOUS_RID => DRUPAL_ANONYMOUS_RID, ), @@ -3105,6 +3104,17 @@ function arg($index = NULL, $path = NULL) { } /** + * Returns the IP address of the client machine. + * + * @return + * IP address of client machine, adjusted for reverse proxy and/or cluster + * environments. + */ +function ip_address() { + return Drupal::service('request')->getClientIp(); +} + +/** * Initializes and returns the class loader. * * The class loader is responsible for lazy-loading all PSR-0 compatible diff --git a/core/includes/session.inc b/core/includes/session.inc index 5c09535..31e67a6 100644 --- a/core/includes/session.inc +++ b/core/includes/session.inc @@ -173,7 +173,7 @@ function _drupal_session_write($sid, $value) { // Either ssid or sid or both will be added from $key below. $fields = array( 'uid' => $user->uid, - 'hostname' => Drupal::service('request')->getClientIP(), + 'hostname' => ip_address(), 'session' => $value, 'timestamp' => REQUEST_TIME, ); diff --git a/core/modules/action/tests/action_loop_test/action_loop_test.module b/core/modules/action/tests/action_loop_test/action_loop_test.module index dfc5226..0c04346 100644 --- a/core/modules/action/tests/action_loop_test/action_loop_test.module +++ b/core/modules/action/tests/action_loop_test/action_loop_test.module @@ -69,7 +69,7 @@ function watchdog_skip_semaphore($type, $message, $variables = array(), $severit 'uid' => isset($user->uid) ? $user->uid : 0, 'request_uri' => $base_root . request_uri(), 'referer' => $_SERVER['HTTP_REFERER'], - 'ip' => Drupal::service('request')->getClientIP(), + 'ip' => '127.0.0.1', 'timestamp' => REQUEST_TIME, ); diff --git a/core/modules/ban/ban.admin.inc b/core/modules/ban/ban.admin.inc index 6def75e..0ea23fc 100644 --- a/core/modules/ban/ban.admin.inc +++ b/core/modules/ban/ban.admin.inc @@ -85,7 +85,7 @@ function ban_ip_form_validate($form, &$form_state) { if (db_query("SELECT * FROM {ban_ip} WHERE ip = :ip", array(':ip' => $ip))->fetchField()) { form_set_error('ip', t('This IP address is already banned.')); } - elseif ($ip == Drupal::service('request')->getClientIP()) { + elseif ($ip == ip_address()) { form_set_error('ip', t('You may not ban your own IP address.')); } elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) { diff --git a/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php b/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php index 8e24181..8d9f2ad 100644 --- a/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php +++ b/core/modules/ban/lib/Drupal/ban/EventSubscriber/BanSubscriber.php @@ -43,7 +43,6 @@ public function __construct(BanIpManager $manager) { * The Event to process. */ public function onKernelRequestBannedIpCheck(GetResponseEvent $event) { - // @todo convert this to Request::getClientIP(). $ip = $event->getRequest()->getClientIp(); if ($this->manager->isDenied($ip)) { $response = new Response('Sorry, ' . check_plain($ip) . ' has been banned.', 403); diff --git a/core/modules/ban/lib/Drupal/ban/Tests/IpAddressBlockingTest.php b/core/modules/ban/lib/Drupal/ban/Tests/IpAddressBlockingTest.php index b058f11..4b40572 100644 --- a/core/modules/ban/lib/Drupal/ban/Tests/IpAddressBlockingTest.php +++ b/core/modules/ban/lib/Drupal/ban/Tests/IpAddressBlockingTest.php @@ -78,7 +78,7 @@ function testIPAddressValidation() { // manually. // TODO: On some systems this test fails due to a bug/inconsistency in cURL. // $edit = array(); - // $edit['ip'] = \Drupal::service('request')->getClientIP(); + // $edit['ip'] = ip_address(); // $this->drupalPost('admin/config/people/ban', $edit, t('Save')); // $this->assertText(t('You may not ban your own IP address.')); } diff --git a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php index aa7df09..77e441f 100644 --- a/core/modules/comment/lib/Drupal/comment/CommentStorageController.php +++ b/core/modules/comment/lib/Drupal/comment/CommentStorageController.php @@ -153,7 +153,7 @@ protected function preSave(EntityInterface $comment) { } // Add the values which aren't passed into the function. $comment->thread->value = $thread; - $comment->hostname->value = \Drupal::service('request')->getClientIP(); + $comment->hostname->value = ip_address(); } } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php index 1edce50..5585650 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentLinksTest.php @@ -145,7 +145,7 @@ function setEnvironment(array $info) { 'uid' => 0, 'status' => COMMENT_PUBLISHED, 'subject' => $this->randomName(), - 'hostname' => \Drupal::service('request')->getClientIP(), + 'hostname' => '127.0.0.1', 'langcode' => LANGUAGE_NOT_SPECIFIED, 'comment_body' => array(LANGUAGE_NOT_SPECIFIED => array($this->randomName())), )); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php index 6d36525..818227e 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentNewIndicatorTest.php @@ -52,7 +52,7 @@ public function testCommentNewCommentsIndicator() { 'uid' => $this->loggedInUser->uid, 'status' => COMMENT_PUBLISHED, 'subject' => $this->randomName(), - 'hostname' => \Drupal::service('request')->getClientIP(), + 'hostname' => '127.0.0.1', 'langcode' => LANGUAGE_NOT_SPECIFIED, 'comment_body' => array(LANGUAGE_NOT_SPECIFIED => array($this->randomName())), )); diff --git a/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php b/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php index 17df6a1..9fcc1ee 100644 --- a/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php +++ b/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php @@ -137,7 +137,7 @@ private function generateLogEntries($count, $type = 'custom', $severity = WATCHD 'uid' => isset($this->big_user->uid) ? $this->big_user->uid : 0, 'request_uri' => $base_root . request_uri(), 'referer' => $_SERVER['HTTP_REFERER'], - 'ip' => \Drupal::service('request')->getClientIP(), + 'ip' => '127.0.0.1', 'timestamp' => REQUEST_TIME, ); $message = 'Log entry added to test the dblog row limit. Entry #'; @@ -424,7 +424,7 @@ protected function testDBLogAddAndClear() { 'uid' => isset($this->big_user->uid) ? $this->big_user->uid : 0, 'request_uri' => $base_root . request_uri(), 'referer' => $_SERVER['HTTP_REFERER'], - 'ip' => \Drupal::service('request')->getClientIP(), + 'ip' => '127.0.0.1', 'timestamp' => REQUEST_TIME, ); // Add a watchdog entry. diff --git a/core/modules/openid/openid.module b/core/modules/openid/openid.module index b23473b..6d31124 100644 --- a/core/modules/openid/openid.module +++ b/core/modules/openid/openid.module @@ -1066,7 +1066,7 @@ function openid_verify_assertion_nonce($service, $response) { return TRUE; } else { - watchdog('openid', 'Nonce replay attempt blocked from @ip, nonce: @nonce.', array('@ip' => Drupal::service('request')->getClientIP(), '@nonce' => $response['openid.response_nonce']), WATCHDOG_CRITICAL); + watchdog('openid', 'Nonce replay attempt blocked from @ip, nonce: @nonce.', array('@ip' => ip_address(), '@nonce' => $response['openid.response_nonce']), WATCHDOG_CRITICAL); return FALSE; } } diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 200ac00..bdf5862 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -1324,7 +1324,7 @@ function user_login_authenticate_validate($form, &$form_state) { // The default identifier is a combination of uid and IP address. This // is less secure but more resistant to denial-of-service attacks that // could lock out all users with public user names. - $identifier = $account->uid . '-' . Drupal::service('request')->getClientIP(); + $identifier = $account->uid . '-' . ip_address(); } $form_state['flood_control_user_identifier'] = $identifier;