@@ -73,13 +73,13 @@ class CI_Input {
7373 */
7474 protected $ headers = array ();
7575
76-
7776 /**
7877 * Constructor
7978 *
8079 * Sets whether to globally enable the XSS processing
8180 * and whether to allow the $_GET array
8281 *
82+ * @return void
8383 */
8484 public function __construct ()
8585 {
@@ -306,50 +306,49 @@ function server($index = '', $xss_clean = FALSE)
306306 /**
307307 * Fetch the IP Address
308308 *
309- * @access public
310309 * @return string
311310 */
312- function ip_address ()
311+ public function ip_address ()
313312 {
314313 if ($ this ->ip_address !== FALSE )
315314 {
316315 return $ this ->ip_address ;
317316 }
318317
319- if (config_item ('proxy_ips ' ) != '' && $ this ->server ('HTTP_X_FORWARDED_FOR ' ) && $ this ->server ('REMOTE_ADDR ' ))
318+ $ proxy_ips = config_item ('proxy_ips ' );
319+ if ( ! empty ($ proxy_ips ))
320320 {
321- $ proxies = preg_split ('/[\s,]/ ' , config_item ('proxy_ips ' ), -1 , PREG_SPLIT_NO_EMPTY );
322- $ proxies = is_array ($ proxies ) ? $ proxies : array ($ proxies );
321+ $ proxy_ips = explode (', ' , str_replace (' ' , '' , $ proxy_ips ));
322+ foreach (array ('HTTP_X_FORWARDED_FOR ' , 'HTTP_CLIENT_IP ' , 'HTTP_X_CLIENT_IP ' , 'HTTP_X_CLUSTER_CLIENT_IP ' ) as $ header )
323+ {
324+ if (($ spoof = $ this ->server ($ header )) !== FALSE )
325+ {
326+ // Some proxies typically list the whole chain of IP
327+ // addresses through which the client has reached us.
328+ // e.g. client_ip, proxy_ip1, proxy_ip2, etc.
329+ if (strpos ($ spoof , ', ' ) !== FALSE )
330+ {
331+ $ spoof = explode (', ' , $ spoof , 2 );
332+ $ spoof = $ spoof [0 ];
333+ }
323334
324- $ this ->ip_address = in_array ($ _SERVER ['REMOTE_ADDR ' ], $ proxies ) ? $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] : $ _SERVER ['REMOTE_ADDR ' ];
325- }
326- elseif ($ this ->server ('REMOTE_ADDR ' ) AND $ this ->server ('HTTP_CLIENT_IP ' ))
327- {
328- $ this ->ip_address = $ _SERVER ['HTTP_CLIENT_IP ' ];
329- }
330- elseif ($ this ->server ('REMOTE_ADDR ' ))
331- {
332- $ this ->ip_address = $ _SERVER ['REMOTE_ADDR ' ];
333- }
334- elseif ($ this ->server ('HTTP_CLIENT_IP ' ))
335- {
336- $ this ->ip_address = $ _SERVER ['HTTP_CLIENT_IP ' ];
337- }
338- elseif ($ this ->server ('HTTP_X_FORWARDED_FOR ' ))
339- {
340- $ this ->ip_address = $ _SERVER ['HTTP_X_FORWARDED_FOR ' ];
341- }
335+ if ( ! $ this ->valid_ip ($ spoof ))
336+ {
337+ $ spoof = FALSE ;
338+ }
339+ else
340+ {
341+ break ;
342+ }
343+ }
344+ }
342345
343- if ($ this ->ip_address === FALSE )
344- {
345- $ this ->ip_address = '0.0.0.0 ' ;
346- return $ this ->ip_address ;
346+ $ this ->ip_address = ($ spoof !== FALSE && in_array ($ _SERVER ['REMOTE_ADDR ' ], $ proxy_ips , TRUE ))
347+ ? $ spoof : $ _SERVER ['REMOTE_ADDR ' ];
347348 }
348-
349- if (strpos ($ this ->ip_address , ', ' ) !== FALSE )
349+ else
350350 {
351- $ x = explode (', ' , $ this ->ip_address );
352- $ this ->ip_address = trim (end ($ x ));
351+ $ this ->ip_address = $ _SERVER ['REMOTE_ADDR ' ];
353352 }
354353
355354 if ( ! $ this ->valid_ip ($ this ->ip_address ))
@@ -642,8 +641,8 @@ function _sanitize_globals()
642641 $ _SERVER ['PHP_SELF ' ] = strip_tags ($ _SERVER ['PHP_SELF ' ]);
643642
644643
645- // CSRF Protection check
646- if ($ this ->_enable_csrf == TRUE )
644+ // CSRF Protection check on HTTP requests
645+ if ($ this ->_enable_csrf == TRUE && ! $ this -> is_cli_request () )
647646 {
648647 $ this ->security ->csrf_verify ();
649648 }
@@ -837,11 +836,11 @@ public function is_ajax_request()
837836 *
838837 * Test to see if a request was made from the command line
839838 *
840- * @return boolean
839+ * @return bool
841840 */
842841 public function is_cli_request ()
843842 {
844- return (php_sapi_name () == 'cli ' ) or defined ('STDIN ' );
843+ return (php_sapi_name () === 'cli ' OR defined ('STDIN ' ) );
845844 }
846845
847846}
0 commit comments