From 75cc2d69149b49fd536a180dc03fc60f61912ae7 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 11 Jun 2017 10:25:48 +0100 Subject: [PATCH] Cleaned up Utils::getIP(), resolved strict-type issues, close #1062 --- src/pocketmine/utils/Utils.php | 58 ++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/pocketmine/utils/Utils.php b/src/pocketmine/utils/Utils.php index a5cfdde71a..aee6d5cb80 100644 --- a/src/pocketmine/utils/Utils.php +++ b/src/pocketmine/utils/Utils.php @@ -126,39 +126,51 @@ class Utils{ * * @param bool $force default false, force IP check even when cached * - * @return string + * @return string|bool */ - public static function getIP($force = false){ if(Utils::$online === false){ return false; }elseif(Utils::$ip !== false and $force !== true){ return Utils::$ip; } - $ip = trim(strip_tags(Utils::getURL("http://checkip.dyndns.org/"))); - if(preg_match('#Current IP Address\: ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ - Utils::$ip = $matches[1]; - }else{ - $ip = Utils::getURL("http://www.checkip.org/"); - if(preg_match('#">([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ - Utils::$ip = $matches[1]; - }else{ - $ip = Utils::getURL("http://checkmyip.org/"); - if(preg_match('#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ - Utils::$ip = $matches[1]; - }else{ - $ip = trim(Utils::getURL("http://ifconfig.me/ip")); - if($ip != ""){ - Utils::$ip = $ip; - }else{ - return false; - } - } + + do{ + $ip = Utils::getURL("http://api.ipify.org/"); + if($ip !== false){ + Utils::$ip = $ip; + break; } - } + + $ip = Utils::getURL("http://checkip.dyndns.org/"); + if($ip !== false and preg_match('#Current IP Address\: ([0-9a-fA-F\:\.]*)#', trim(strip_tags($ip)), $matches) > 0){ + Utils::$ip = $matches[1]; + break; + } + + $ip = Utils::getURL("http://www.checkip.org/"); + if($ip !== false and preg_match('#">([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ + Utils::$ip = $matches[1]; + break; + } + + $ip = Utils::getURL("http://checkmyip.org/"); + if($ip !== false and preg_match('#Your IP address is ([0-9a-fA-F\:\.]*)#', $ip, $matches) > 0){ + Utils::$ip = $matches[1]; + break; + } + + $ip = Utils::getURL("http://ifconfig.me/ip"); + if($ip !== false and trim($ip) != ""){ + Utils::$ip = trim($ip); + break; + } + + return false; + + }while(false); return Utils::$ip; - } /**