includes/functions.php
changeset 1251 d543689ed2eb
parent 1249 81b03b3e88d0
child 1253 13f8d373da67
equal deleted inserted replaced
1250:d2db9f3628ab 1251:d543689ed2eb
  2872  * @return bool true if valid, false otherwise
  2872  * @return bool true if valid, false otherwise
  2873  */
  2873  */
  2874 
  2874 
  2875 function is_valid_ip($ip)
  2875 function is_valid_ip($ip)
  2876 {
  2876 {
       
  2877 	return is_valid_ipv4($ip) || is_valid_ipv6($ip);
       
  2878 }
       
  2879 
       
  2880 /**
       
  2881  * Test validity of IPv4 address
       
  2882  * @param string
       
  2883  * @return bool
       
  2884  */
       
  2885 
       
  2886 function is_valid_ipv4($ip)
       
  2887 {
  2877 	// This next one came from phpBB3.
  2888 	// This next one came from phpBB3.
  2878 	$ipv4 = '(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])';
  2889 	$ipv4 = '(?:(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])';
       
  2890 	return preg_match("/^{$ipv4}$/", $ip) ? true : false;
       
  2891 }
       
  2892 
       
  2893 /**
       
  2894  * Test validity of IPv6 address
       
  2895  * @param string
       
  2896  * @return bool
       
  2897  */
       
  2898 
       
  2899 function is_valid_ipv6($ip)
       
  2900 {
  2879 	$ipv6 = '(?:[a-f0-9]{0,4}):(?:[a-f0-9]{0,4}):(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{1,4})';
  2901 	$ipv6 = '(?:[a-f0-9]{0,4}):(?:[a-f0-9]{0,4}):(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{0,4}:|:)?(?:[a-f0-9]{1,4})';
  2880 
  2902 	return preg_match("/^{$ipv6}$/", $ip) ? true : false;
  2881 	if ( preg_match("/^{$ipv4}$/", $ip) || preg_match("/^{$ipv6}$/", $ip) )
       
  2882 		return true;
       
  2883 	else
       
  2884 		return false;
       
  2885 }
  2903 }
  2886 
  2904 
  2887 /**
  2905 /**
  2888  * Replaces the FIRST given occurrence of needle within haystack with thread
  2906  * Replaces the FIRST given occurrence of needle within haystack with thread
  2889  * @param string Needle
  2907  * @param string Needle
  5337 		$crypto_backend = 'gmp';
  5355 		$crypto_backend = 'gmp';
  5338 	
  5356 	
  5339 	return $crypto_backend;
  5357 	return $crypto_backend;
  5340 }
  5358 }
  5341 
  5359 
       
  5360 /**
       
  5361  * Perform X-Forwarded-For check and apply it as the REMOTE_ADDR if the settings tell us to
       
  5362  */
       
  5363 
       
  5364 function do_xff_check()
       
  5365 {
       
  5366 	if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) && getConfig('trust_xff', 'none') != 'none' )
       
  5367 	{
       
  5368 		switch(getConfig('trust_xff', 'none'))
       
  5369 		{
       
  5370 			case 'both':
       
  5371 				if ( is_valid_ip($_SERVER['HTTP_X_FORWARDED_FOR']) )
       
  5372 					$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
       
  5373 				break;
       
  5374 			case 'ipv4':
       
  5375 				if ( is_valid_ip($_SERVER['HTTP_X_FORWARDED_FOR']) && is_valid_ipv4($_SERVER['REMOTE_ADDR']) )
       
  5376 					$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
       
  5377 				break;
       
  5378 			case 'ipv6':
       
  5379 				if ( is_valid_ip($_SERVER['HTTP_X_FORWARDED_FOR']) && is_valid_ipv6($_SERVER['REMOTE_ADDR']) )
       
  5380 					$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
       
  5381 				break;
       
  5382 		}
       
  5383 	}
       
  5384 }