Преглед на файлове

ZF-7117: Add a flag to getClientIp to check for HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR or not.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16304 44c647ce-9c0f-0410-b52a-842ac1e357ba
norm2782 преди 16 години
родител
ревизия
574f941a4e
променени са 2 файла, в които са добавени 18 реда и са изтрити 14 реда
  1. 4 3
      library/Zend/Controller/Request/Http.php
  2. 14 11
      tests/Zend/Controller/Request/HttpTest.php

+ 4 - 3
library/Zend/Controller/Request/Http.php

@@ -1000,15 +1000,16 @@ class Zend_Controller_Request_Http extends Zend_Controller_Request_Abstract
      *
      *
      * @return string
      * @return string
      */
      */
-    public function getClientIp()
+    public function getClientIp($checkProxy = true)
     {
     {
-        if ($this->getServer('HTTP_CLIENT_IP') != null) {
+        if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
             $ip = $this->getServer('HTTP_CLIENT_IP');
             $ip = $this->getServer('HTTP_CLIENT_IP');
-        } else if ($this->getServer('HTTP_X_FORWARDED_FOR') != null) {
+        } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
             $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
             $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
         } else {
         } else {
             $ip = $this->getServer('REMOTE_ADDR');
             $ip = $this->getServer('REMOTE_ADDR');
         }
         }
+
         return $ip;
         return $ip;
     }
     }
 }
 }

+ 14 - 11
tests/Zend/Controller/Request/HttpTest.php

@@ -705,17 +705,20 @@ class Zend_Controller_Request_HttpTest extends PHPUnit_Framework_TestCase
         $_SERVER['REMOTE_ADDR'] = '192.168.1.12';
         $_SERVER['REMOTE_ADDR'] = '192.168.1.12';
 
 
         $this->assertEquals('192.168.1.12', $request->getClientIp());
         $this->assertEquals('192.168.1.12', $request->getClientIp());
-//    public function getClientIp()
-//{
-//   if (!empty($this->getServer('HTTP_CLIENT_IP'))) {
-//       $ip = $this->getServer('HTTP_CLIENT_IP');
-//   } else if (!empty($this->getServer('HTTP_X_FORWARDED_FOR'))) {
-//       $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
-//   } else {
-//       $ip = $this->getServer('REMOTE_ADDR');
-//   }
-//   return $ip;
-//}
+    }
+
+    /**
+     * @group ZF-7117
+     */
+    public function testGetClientIpNoProxyCheck()
+    {
+        $request = new Zend_Controller_Request_Http();
+
+        $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.10';
+        $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.11';
+        $_SERVER['REMOTE_ADDR'] = '192.168.1.12';
+
+        $this->assertEquals('192.168.1.12', $request->getClientIp(false));
     }
     }
 }
 }